12- MySQL Select Data

0
45

MySQL Select Data is used In Selecting one or more records in MySQL database, the SELECT statement is being used.

Syntax:

SELECT * from tableName;

or

SELECT column1,column2  from tableName;

 

The asterisks(*) symbol in our first example syntax represent all column in the database table.

In the second example, we specify the column name of a table.

 

Example using MySQLi (Object-Oriented):

  1. <!--?php $server = "localhost"; $username = "root"; $password = ""; $database = "mysqltutorial"; // Create connection $conn = new mysqli($server, $username, $password, $database); // Check connection if ($conn-&gt;connect_error) {&lt;br ?--> die("Connection failed: " . $conn-&gt;connect_error);
  2. }
  3.  
  4. // sql to Insert record table
  5. $sql = "SELECT <code>LNAME</code>, <code>FNAME</code>, <code>ADDRESS</code> from <code>tblpeople</code>";
  6. $result = $conn-&gt;query($sql);
  7. echo "
  8.  
  9. ";
  10. if ($result-&gt;num_rows &gt; 0) {// output data of each row
  11. while($row = $result-&gt;fetch_assoc()) {
  12. echo "";
  13. }
  14. } else {
  15. echo "0 results";
  16. }echo "
  17. <table border="1">
  18. <tbody>
  19. <tr>
  20. <th>LAST NAME</th>
  21. <th>FIRST NAME</th>
  22. <th>ADDRESS</th>
  23. </tr>
  24. <tr>
  25. <td>" . $row["LNAME"]. "</td>
  26. <td>" . $row["FNAME"]. "</td>
  27. <td>" . $row["ADDRESS"]. "</td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. ";
  32. $conn-&gt;close();
  33. ?&gt;

Code explanation:

We just simply Select the Last name, First Name and Address of a person from our table, then we execute the query and store the results to the “$result” variable.

Next, using the num_rows function check if the result is greater than zero. And if the result is greater than zero, the fetch_assoc() function will store the result into an associative array so that we will be able to loop through the records.

The While Loop Loops through the result set and display the result inside the table data because we use table to display the result.

 

Example using MySQLi(Procedural):

  1. <!--?php &lt;br ?--> $server = "localhost";
  2. $username = "root";
  3. $password = "";
  4. $database = "mysqltutorial";
  5.  
  6. // Create connection
  7. $conn = mysqli_connect($server, $username, $password, $database);
  8. // Check connection
  9. if (!$conn) {
  10. die("Connection failed: " . mysqli_connect_error());
  11. }
  12.  
  13. // sql to Insert record table
  14. $sql = "SELECT <code>LNAME</code>, <code>FNAME</code>, <code>ADDRESS</code> from <code>tblpeople</code>";
  15. $results = mysqli_query($conn , $sql);
  16. echo "
  17.  
  18. ";
  19. if (mysqli_num_rows($results) &gt; 0) {// output data of each row
  20. while($row = mysqli_fetch_assoc($results)) {
  21. echo "";
  22. }
  23. } else {
  24. echo "0 results";
  25. }echo "
  26. <table border="1">
  27. <tbody>
  28. <tr>
  29. <th>LAST NAME</th>
  30. <th>FIRST NAME</th>
  31. <th>ADDRESS</th>
  32. </tr>
  33. <tr>
  34. <td>" . $row["LNAME"]. "</td>
  35. <td>" . $row["FNAME"]. "</td>
  36. <td>" . $row["ADDRESS"]. "</td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. ";
  41. mysqli_close($conn);
  42. ?&gt;

Example using PDO:

  1. <!--?php $server = "localhost"; $username = "root"; $password = ""; $database = "mysqltutorial"; try { $conn = new PDO("mysql:host=$server;dbname=$database", $username, $password); // set the PDO error mode to exception $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);&lt;/p&gt;
  2. &lt;p&gt;   // sql to Select record from table&lt;br ?--> $sql = "SELECT <code>LNAME</code>, <code>FNAME</code>, <code>ADDRESS</code> from <code>tblpeople</code>";
  3. $query = $conn-&gt;prepare( $sql );
  4. $query-&gt;execute();
  5. $results = $query-&gt;fetchAll( PDO::FETCH_ASSOC );
  6.  
  7. ?&gt;
  8.  
  9. foreach( $results as $row ){
  10. echo "";
  11. }
  12. }
  13. catch(PDOException $e) {
  14. echo "Error: " . $e-&gt;getMessage();
  15. }
  16. $conn = null;
  17. echo "
  18. <table border="1">
  19. <tbody>
  20. <tr>
  21. <th>LAST NAME</th>
  22. <th>FIRST NAME</th>
  23. <th>ADDRESS</th>
  24. </tr>
  25. <!--?php &lt;br ?-->
  26. <tr>
  27. <td>" . $row["LNAME"]. "</td>
  28. <td>" . $row["FNAME"]. "</td>
  29. <td>" . $row["ADDRESS"]. "</td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. ";
  34. ?&gt;

The Output of Code above looks like as shown below.

select record

Our Previous Topic called: 11- MySQL Multiple Insert

 

Facebook Comments
  • Login
    Login
  • select record
    select record

LEAVE A REPLY

Please enter your comment!
Please enter your name here