‘Select’ statement is used to select record from MySQL database server.Synatx: select column_name  from  table_name

Let’s have an example, how to select record from MySQL database server.

Example:
<?php
    // creatingconnection with mysql database server
 
    $con = mysql_connect("localhost", "root", "root");
    // check connection is establish or not
    if(!$con)
    {
        die ("Connectionnot established! Please try again");
    }
 
    echo '<b>Connection successfully open</b>'.'</br>';
    // selecting database
     mysql_select_db("MySqlDatabase", $con);
    // selectingrecord
    $qry = "Select * from mysqldatabse.tbllogin";
 
    // printselecting record
    $qryResult= mysql_query($qry);
 
    while ($row = mysql_fetch_array($qryResult))
    {
        echo $row['Name'].'   '.$row['EmailId'].'   '.$row['Password'];
        echo '</br>';
    }
 
    // Closingthe connection
 
    mysql_close($con);
    echo '<b>Connection closed successfully</b>';
?>


Note: mysql_query() function sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier and mysql_fetch_array() function returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Output:

Selecting record from MySQL database in PHP

Note: ‘*’ is used for selecting all records from table.

You can select specific column record.

<?php
    // creatingconnection with mysql database server
 
    $con = mysql_connect("localhost", "root", "root");
    // check connection is establish or not
    if(!$con)
    {
        die ("Connectionnot established! Please try again");
    }
 
    echo '<b>Connection successfully open</b>'.'</br>';
    // selecting database
     mysql_select_db("MySqlDatabase", $con);
    // selectingrecord with specified column name
    $qry = "Select Name,EmailId,Password from mysqldatabse.tbllogin";
 
    $qryResult= mysql_query($qry);
    // printselecting record
    while ($row = mysql_fetch_array($qryResult))
    {
        print_r($row);
        echo '</br>';
    }
    //Closingthe connection
    mysql_close($con);
    echo '<b>Connection closed successfully</b>';
?>

Note: print_r() function is used for print array.

Output:

Selecting record from MySQL database in PHP

Example: Selecting record with ‘where’ clause
<?php
    // creatingconnection with mysql database server
 
    $con = mysql_connect("localhost", "root", "root");
    // check connection is establish or not
    if(!$con)
    {
        die ("Connectionnot established! Please try again");
    }
 
    echo '<b>Connection successfully open</b>'.'</br>';
    // selecting database
     mysql_select_db("MySqlDatabase", $con);
    // selectingrecord with where clause
    $qry = "Select Password from mysqldatabse.tbllogin where EmailId ='arunsingh@yahoomail.com'";
 
    $qryResult= mysql_query($qry);
    // printselecting record
    while ($row = mysql_fetch_array($qryResult))
    {
       echo 'Password is : '.$row['Password'];
        echo '</br>';
    }
    //Closingthe connection
    mysql_close($con);
    echo '<b>Connection closed successfully</b>';
?>
Output:

Selecting record from MySQL database in PHP


  Modified On Nov-23-2017 03:43:31 AM

Leave Comment