How to Update value in the mysql table step by step guide for beginner


Update Query in Sql to update value in Mysql table | In this article we learn how to update value in the mysql table using update query in sql. In an organization it is required to change or modify the date frequently into the table.

Update data in the mysql table using update query in sql

To update query in sql table using mysql promt we use”UPDATE”command as below.

Lets take an example to update data in the student table which we previously created.
Before update to view the table data using the below command.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Sharma          |
+----------------+------------------+-----------------+
1 row in set (0.01 sec)

Now we are going to update the last name of the student whose roll no is 1. To do this we use the below command.

mysql> UPDATE student SET studentlastname = 'Ghosh' WHERE studentrollno = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

After modified the date we need to view the data using the below command.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentnrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Ghosh           |
+----------------+------------------+-----------------+
1 row in set (0.00 sec)
How to Update value in the mysql table step by step guide for beginner,update query in sql
How to Update value in the mysql table step by step guide for beginner 

Update data in mysql table using php script

To update the data using php script we have to take mysql_query function as below.


To connect to mysql server we use mysql_connect function and to select the database we will use mysql_select_db function(if we not define db name variable).All functions are important to run update query in sql using php script. 

To update data into mysql table we need to pass two arguments (connection and query)inside the mysql_query function. 

Lets take an example to update the data using php script

<html>
   <head>
      <title>Update value into MySQL table on linux system</title>
   </head>
   <body>
      <?php
         $mysql_host = 'localhost';
         $mysql_user = 'root';
         $mysql_pass = 'passwordofroot';
         $dbname = "techrideradmin";
         $conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass,$dbname);
        
         if(! $conn ) {
            die('Could not connect the server: ' . mysql_error());
         }
         echo 'Connection successfully established';
         $sql = “UPDATE student SET studentlastname = 'Ghosh' WHERE studentrollno = 1;”;
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('value not updated: ' . mysql_error());
         }
         echo "Value updated succcessfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

That’s all.If this article is helpful to know about Update Query in Sql to update value in Mysql table please share it!!!!

SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment