+

PHP MySQL Select Query

Syntax:
Basic syntax of select clause - To select all columns from a table, the character is in use. Implementation of the Select query:
Let`s look at the following table "Data" with three columns "FirstName", "LastName" and "Age". To select all data stored in the "Data" table, we will use the code below.SELECT request with and using a procedural method: $link = mysqli_connect ( "localhost" , "root" , "" , " Mydb " ); if ($link == = false ) { die ( "ERROR: Could not connect." . mysqli_connect_error()); } $sql = "SELECT * FROM Data" ; if ($res = mysqli_query ($link, $sql)) { if (mysqli_num_rows ($res) > 0) { echo " < table > " ; echo "< tr > " ; echo "< th > Firstname < / th > " ; echo "< th > Lastname < / th > " ; echo "< th > age < / th > " ; echo "< / tr > " ; while ($row = mysqli_fetch_array ($res)) { echo " < tr > " ; echo "< td > " . $row [ ` Firstname` ]. "< / td >" ; echo "< td > " . $row [ ` Lastname` ]. "< / td >" ; echo "< td > " . $row [ ` Age` ]. "< / td >" ; echo "< / tr > " ; } echo " < / table > " ; mysqli_free_res ($res); } else { echo "No matching records are found." ; } } else { echo "ERROR: Could not be able to execute $sql." . mysqli_error ($link); }
mysqli_close ($link);
?>
Output: Code explanation:
  • The "res" variable stores the data returned by mysql_query().
  • Each times, when mysqli_fetch_array() is called, it returns the next row from the set res().
  • The while loop is used to loop through all rows in the "data" table.
  • SELECT the query using the object-oriented method: php $ mysqli = new mysqli (" localhost "," root "," "," Mydb ");  
    if ($mysqli == = false) { die ("ERROR: Could not connect." . $mysqli-> connect_error); }  
    $sql = "SELECT * FROM Data";
    if ($res = $mysqli-> query ($sql)) { if ($res-> num_rows > 0) {   echo "< table >"; echo "< tr < / code> > "; echo "< th < / code> > Firstname < / th > "; echo "< th < / code> > Lastname < / th > "; echo "< th < / code> > Age < / th > "; echo "< / tr > "; while ($row = $res-> fetch_array()) {  < / code> echo "< tr >"; echo "< td < / code> > ". $row [` Firstname`]. "< / td >"; echo "< td < / code> > ". $row [` Lastname`]. "< / td >"; echo "< td < / code> > ". $row [` Age`]. "< / td >"; echo "< / tr > "; } echo" < / table > "; $res-> free(); } else { echo "No matching records are found."; } }
    else { echo" ERROR: Could not be able to execute $sql. " . $mysqli-> error; }
    $mysqli-> close();
    ?>
    Output: SELECT Request using PDO method: php 
    try { $ pdo = new PDO (" mysql: host = localhost ; dbname = mydb "," root "," "); $pdo-> setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }
    catch (PDOException $e) { die ("ERROR: Could not connect.". $e-> getMessage()); }
    try { $sql = "SELECT * FROM Data"; $res = $pdo-> query ($sql); if ($res-> rowCount() > 0) { echo "< table > "; echo "< tr < / code> > "; echo "< th < / code> > Firstname < / th > "; echo "< th < / code> > Lastname < / th > "; echo "< th < / code> > Age < / th > "; echo "< / tr > "; while ($row = $res-> fetch()) { echo "< tr > "; echo "< td < / code> > ". $row [` Firstname`]. "< / td >"; echo "< td < / code> > ". $row [` Lastname`]. "< / td >"; echo "< td < / code> > ". $row [` Age`]. "< / td >"; echo "< / tr > "; } echo" < / table > "; unset ($res); } else { echo "No matching records are found."; } }
    catch (PDOException $e) { die ("ERROR: Could not be able to execute $sql." . $e-> getMessage()); }
    unset ($pdo);
    ?>
    Output:
    Get Solution for free from DataCamp guru