How to Delete value from the mysql table step by step guide for beginner


In this article we learn how to delete value from the mysql table using delete query in sql. In an organization it is required to delete the date from the mysql table .

Delete data from the mysql table using mysql promt

To delete data from the mysql table using delete query we use”DELETE FROM”command as below.

Lets take an example to delete the date which we previously created.

Before delete to view the table data using the below command.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentnrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Ghosh           |
+----------------+------------------+-----------------+
1 row in set (0.00 sec)

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

mysql> DELETE FROM student WHERE studentnrollno=1;
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from student;
Empty set (0.01 sec)
How to Delete value from the mysql table step by step guide for beginner
How to Delete value from the mysql table step by step guide for beginner

Delete data from mysql table using php script

To delete the data using php script we use 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 dbname variable not defined).All functions are important to run delete query in sql using php script. 


To delete data from mysql table we need to pass two arguments inside the mysql_query function. 

Lets take an example to delete the data using php script

<html>
   <head>
      <title>Delete 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 = “DELETE FROM student WHERE studentrollno=1;”;
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('value not deleted: ' . mysql_error());
         }
         echo "Value deleted succcessfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>


That’s all.If this article is helpful please share it!!!!


SHARE

Admin

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

0 comments:

Post a Comment