Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Select Data From MySQL
 
Select Data From a MySQL Database
 
The SELECT statement is used to select data from one or more tables: 
  1. SELECT column_name(s) FROM table_name
or we can use the * character to select ALL columns from a table:
  1. SELECT * FROM table_name
 
Select Data With MySQLi
 
The following example selects the id, firstname and lastname columns from the MyGuests table and displays it on the page:
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10.     die("Connection failed: " . $conn->connect_error);
  11. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  12. $result = $conn->query($sql);
  13. if ($result->num_rows > 0) {
  14.     // output data of each row
  15.     while($row = $result->fetch_assoc()) {
  16.         echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  17.     }
  18. } else {
  19.     echo "0 results";
  20. }
  21. $conn->close();
  22. ?>
 
Code lines to explain from the example above:
 
First, we set up an SQL query that selects the id, firstname and lastname columns from the MyGuests table. The next line of code runs the query and puts the resulting data into a variable called $result.
 
Then, the function num_rows() checks if there are more than zero rows returned.
 
If there are more than zero rows returned, the function fetch_assoc() puts all the results into an associative array that we can loop through. The while() loop loops through the result set and outputs the data from the id, firstname and lastname columns.
 
The following example shows the same as the example above, in the MySQLi procedural way:
 
Example (MySQLi Procedural)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);
  8. // Check connection
  9. if (!$conn) {
  10.     die("Connection failed: " . mysqli_connect_error());
  11. }
  12. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  13. $result = mysqli_query($conn, $sql);
  14. if (mysqli_num_rows($result) > 0) {
  15.     // output data of each row
  16.     while($row = mysqli_fetch_assoc($result)) {
  17.         echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
  18.     }
  19. } else {
  20.     echo "0 results";
  21. }
  22. mysqli_close($conn);
  23. ?>
You can also put the result in an HTML table:
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10.     die("Connection failed: " . $conn->connect_error);
  11. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  12. $result = $conn->query($sql);
  13. if ($result->num_rows > 0) {
  14.     echo "<table><tr><th>ID</th><th>Name</th></tr>";
  15.     // output data of each row
  16.     while($row = $result->fetch_assoc()) {
  17.         echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";
  18.     }
  19.     echo "</table>";
  20. } else {
  21.     echo "0 results";
  22. }
  23. $conn->close();
  24. ?>
 
Select Data With PDO (+ Prepared Statements)
 
The following example uses prepared statements.
 
It selects the id, firstname and lastname columns from the MyGuests table and displays it in an HTML table:
 
Example (PDO)
  1. <?php
  2. echo "<table style='border: solid 1px black;'>";
  3. echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";
  4. class TableRows extends RecursiveIteratorIterator { 
  5.     function __construct($it) { 
  6.         parent::__construct($it, self::LEAVES_ONLY); 
  7.     }
  8.     function current() {
  9.         return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
  10.     }
  11.     function beginChildren() { 
  12.         echo "<tr>"; 
  13.     } 
  14.     function endChildren() { 
  15.         echo "</tr>" . " ";
  16.     } 
  17. $servername = "localhost";
  18. $username = "username";
  19. $password = "password";
  20. $dbname = "myDBPDO";
  21. try {
  22.     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  23.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  24.     $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests"); 
  25.     $stmt->execute();
  26.  
  27.     // set the resulting array to associative
  28.     $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
  29.     foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
  30.         echo $v;
  31.     }
  32. }
  33. catch(PDOException $e) {
  34.     echo "Error: " . $e->getMessage();
  35. }
  36. $conn = null;
  37. echo "</table>";
  38. ?>
 


2016-12-10
461 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский