MySQL Select Query

select query in my sql

MySQL Select Query : The SELECT statement in MySQL is used to fetch data from one or more tables. We can retrieve records of all fields or specified fields that match specified criteria using this statement. It can also work with various scripting languages such as PHP, Ruby, and many more.


MySQL Select Query Syntax

Here is generic SQL syntax of SELECT command to fetch data from the MySQL table −

SELECT field1, field2,...fieldN 

FROM table_name1, table_name2...

[WHERE Clause]

[OFFSET M ][LIMIT N]

Fetching Data from a Command Prompt

This will use SQL SELECT command to fetch data from the MySQL table edusites_tbl.

Example

The following example will return all the records from the edusites_tbl table −

root@host# mysql -u root -p password;

Enter password:*******

mysql use edusiteS;

Database changed

mysql SELECT * from edusites_tbl 

+-------------+----------------+-----------------+-----------------+

| tutorial_id | tutorial_title | tutorial_author | submission_date |

+-------------+----------------+-----------------+-----------------+

|           1 | Learn PHP      | John Poul       | 2007-05-21      |

|           2 | Learn MySQL    | Abdul S         | 2007-05-21      |

|           3 | JAVA Tutorial  | Sanjay          | 2007-05-21      |

+-------------+----------------+-----------------+-----------------+

3 rows in set (0.01 sec)



mysql>

Fetching Data Using a PHP Script

You can use the same SQL SELECT command into a PHP function mysql_query().

The following program is a simple example which will show how to fetch / display records from the edusite_tbl table.

Example

<?php

   $dbhost = 'localhost:3036';

   $dbuser = 'root';

   $dbpass = 'rootpassword';

   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   

   if(! $conn ) {

      die('Could not connect: ' . mysql_error());

   }

   $sql = 'SELECT edusite_id, edusite_title, edusite_author, submission_date FROM edusites_tbl';



   mysql_select_db('edusite');

   $retval = mysql_query( $sql, $conn );

   

   if(! $retval ) {

      die('Could not get data: ' . mysql_error());

   }

   

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {

      echo "edusite ID :{$row['edusite_id']}  <br> ".

         "Title: {$row['edusite_title']} <br> ".

         "Author: {$row['edusite_author']} <br> ".

         "Submission Date : {$row['submission_date']} <br> ".

         "--------------------------------<br>";

   } 

   echo "Fetched data successfully\n";

   mysql_close($conn);

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *