CN5109 WEB APPLICATION DEVELOPMENT Chapter 8 MySQLi PHP Syntax – Working with Data
MySQLi Insert Data • To insert data into MySQLi table, you would need to use SQL INSERT INTO command. • You can use INSERT INTO command into PHP function mysqli_query() to insert data into a MySQLi table.
MySQLi Insert Data • Example: /* HTML FORM*/ <form action = “ " method = "post"> <label>Full Name :</label> <input type = "text" name = “fname" /> <br /> <br /> <input type = "submit" value ="Submit" name = "submit"/> <br /> </form>
MySQLi Insert Data • Example: /* PHP FILE */ <?php // Connection to Database // Select Database $sql = "INSERT INTO table1(fname)VALUES ('".$_POST[“fname"]."')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . “ " . mysqli_error($conn); } mysqli_close($conn); ?>
MySQLi Query Data • The SQL SELECT command is used to fetch data from MySQLi database. • You can use same SQL SELECT command into PHP function mysqli_query(). • This function is used to execute SQL command and later another PHP function mysqli_fetch_assoc() can be used to fetch all the selected data. • This function returns row as an associative array, a numeric array, or both. • This function returns FALSE if there are no more rows.
MySQLi Query Data • Example: <?php // Connection to Database // Select Database $sql = 'SELECT fname FROM table1'; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row["name"]. "<br>"; } } else { echo "0 result"; } mysqli_close($conn); ?>
MySQLi Query Data • We have seen SQL SELECT command to fetch data from MySQLi table. • We can use a conditional clause called WHERE clause to filter out results. • Using WHERE clause, we can specify a selection criteria to select required records from a table. • The WHERE clause works like an if condition in any programming language. This clause is used to compare given value with the field value available in MySQLi table. • If given value from outside is equal to the available field value in MySQLi table, then it returns that row. • You can use same SQL SELECT command with WHERE CLAUSE into PHP function mysqli_query().
MySQLi Query Data • Example: <?php // Connection to Database // Select Database $sql = 'SELECT fname FROM table1 WHERE fname=“ahmad”'; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row["name"]. "<br>"; } } else { echo "0 result"; } mysqli_close($conn); ?>
MySQLi UPDATE Data • There may be a requirement where existing data in a MySQLi table needs to be modified. • You can do so by using SQL UPDATE command. • This will modify any field value of any MySQLi table. • You can update one or more field altogether. • You can specify any condition using WHERE clause. • You can update values in a single table at a time. • You can use SQL UPDATE command with or without WHERE CLAUSE into PHP function mysqli_query().
MySQLi UPDATE Data • Example: <?php // Connection to Database // Select Database $sql = ' UPDATE table1 SET fname="althamas" WHERE name="ram"'; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } mysqli_close($conn); ?>
MySQLi DELETE Data • If you want to delete a record from any MySQLi table, then you can use SQL command DELETE FROM. • If WHERE clause is not specified, then all the records will be deleted from the given MySQLi table. • You can specify any condition using WHERE clause. • You can delete records in a single table at a time. • You can use SQL DELETE command with or without WHERE CLAUSE into PHP function mysqli_query().

Web Application Development using PHP Chapter 8

  • 1.
  • 2.
    MySQLi Insert Data •To insert data into MySQLi table, you would need to use SQL INSERT INTO command. • You can use INSERT INTO command into PHP function mysqli_query() to insert data into a MySQLi table.
  • 3.
    MySQLi Insert Data •Example: /* HTML FORM*/ <form action = “ " method = "post"> <label>Full Name :</label> <input type = "text" name = “fname" /> <br /> <br /> <input type = "submit" value ="Submit" name = "submit"/> <br /> </form>
  • 4.
    MySQLi Insert Data •Example: /* PHP FILE */ <?php // Connection to Database // Select Database $sql = "INSERT INTO table1(fname)VALUES ('".$_POST[“fname"]."')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . “ " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 5.
    MySQLi Query Data •The SQL SELECT command is used to fetch data from MySQLi database. • You can use same SQL SELECT command into PHP function mysqli_query(). • This function is used to execute SQL command and later another PHP function mysqli_fetch_assoc() can be used to fetch all the selected data. • This function returns row as an associative array, a numeric array, or both. • This function returns FALSE if there are no more rows.
  • 6.
    MySQLi Query Data •Example: <?php // Connection to Database // Select Database $sql = 'SELECT fname FROM table1'; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row["name"]. "<br>"; } } else { echo "0 result"; } mysqli_close($conn); ?>
  • 7.
    MySQLi Query Data •We have seen SQL SELECT command to fetch data from MySQLi table. • We can use a conditional clause called WHERE clause to filter out results. • Using WHERE clause, we can specify a selection criteria to select required records from a table. • The WHERE clause works like an if condition in any programming language. This clause is used to compare given value with the field value available in MySQLi table. • If given value from outside is equal to the available field value in MySQLi table, then it returns that row. • You can use same SQL SELECT command with WHERE CLAUSE into PHP function mysqli_query().
  • 8.
    MySQLi Query Data •Example: <?php // Connection to Database // Select Database $sql = 'SELECT fname FROM table1 WHERE fname=“ahmad”'; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "Name: " . $row["name"]. "<br>"; } } else { echo "0 result"; } mysqli_close($conn); ?>
  • 9.
    MySQLi UPDATE Data •There may be a requirement where existing data in a MySQLi table needs to be modified. • You can do so by using SQL UPDATE command. • This will modify any field value of any MySQLi table. • You can update one or more field altogether. • You can specify any condition using WHERE clause. • You can update values in a single table at a time. • You can use SQL UPDATE command with or without WHERE CLAUSE into PHP function mysqli_query().
  • 10.
    MySQLi UPDATE Data •Example: <?php // Connection to Database // Select Database $sql = ' UPDATE table1 SET fname="althamas" WHERE name="ram"'; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } mysqli_close($conn); ?>
  • 11.
    MySQLi DELETE Data •If you want to delete a record from any MySQLi table, then you can use SQL command DELETE FROM. • If WHERE clause is not specified, then all the records will be deleted from the given MySQLi table. • You can specify any condition using WHERE clause. • You can delete records in a single table at a time. • You can use SQL DELETE command with or without WHERE CLAUSE into PHP function mysqli_query().