How to write PHP script to update an existing MySQL table?

MySQLMySQLi DatabasePHP

We can use the SQL UPDATE command with or without the WHERE CLAUSE in the PHP function – mysql_query(). This function will execute the SQL command in a similar way it is executed at the mysql> prompt. To illustrate it we are having the following example −

Example

In this example, we are writing a PHP script to update the field named tutorial_title for a record having turorial_id as 3.

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }
   
   $sql = 'UPDATE tutorials_tbl SET tutorial_title="Learning JAVA" WHERE tutorial_id=3';
   mysql_select_db('TUTORIALS');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not update data: ' . mysql_error());
   }
   echo "Updated data successfully\n";
   mysql_close($conn);
?>
raja
Published on 22-Feb-2018 11:06:55
Advertisements