How to select value(fetch data) from MySQL table step by step guide for beginner


In this tutorial we learn how to select value or fetch data from mysql table . To fetch data from mysql table we have two methods.
1)Fetch data through mysql promt using select command.
2)Fetch data using php script.

Fetch data from mysql table using mysql promt

To fetch data from mysql table through mysql promt we use “select” command.Lets take an example to fetch data using mysql promt. We have mysql table called student from this table we fetch the data as below.

mysql> select * from student;
+----------------+------------------+-----------------+
| studentnrollno | studentfirstname | studentlastname |
+----------------+------------------+-----------------+
|              1 | Raktim           | Sharma          |
+----------------+------------------+-----------------+
1 row in set (0.01 sec)
How to select value(fetch data) from MySQL table step by step guide for beginner
How to select value(fetch data) from MySQL table step by step guide for beginner 
From the above example to view all field we used”*”. If we want to find specific field we can use the below command. In this below command we find the student first name from student table.

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

Fetch data from mysql table using php script

To fetch data from mysql table we use two function.one is mysql_query for sql queries and other is mysql_fetch_array to fetch the data from mysql table.
Lets take an example to fetch data from mysql table using php script.
   <html>
   <head>
      <title>Fetch data from 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 = “SELECT studentrollno,studentfirstname, studentlastname”;
         $result = mysql_query( $sql, $conn );
        
         if( $result > 0 ) {
            while($row = mysql_fetch_array($result)) {
        echo "Roll no: " . $row["studentrollno"]. "  Name: " . $row["studentfirstname"]. " " . $row["studentlastname"]. "<br>";
             }
             }
         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