Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. <?php
  2.  
  3. $mysqli = new mysqli('localhost','user','pass','db');
  4. //This is where we will query the database and pull using the cities/states SELECT statement
  5. // If the result returns true
  6. if ($result = $mysqli->query("SELECT * FROM users")) {
  7. // So the result returned true, let's loop and print out each city.
  8. // The number of rows returned is assigned to the property "num_rows" in the mysql_result class
  9. echo 'There are '.$result->num_rows.' users within the user database.
  10. ';
  11. // The "fetch_object()" method is the equivalent of the old mysql_fetch_object() function. It allows access to the returned rows within the resouce object ($result in this case).
  12. while ($row = $result->fetch_object()) {
  13. echo 'Email: '.$row->emailaddress.'
  14. ';
  15. }
  16. } else {
  17. // Notice below that the errors are still contained within the mysqli class. This means that each result will affect a single "error" property. In otherwords, if your result fails, the error returned from MySQL is assigned to the property "error".
  18. // This means the query failed
  19. echo $mysqli->error;
  20. } // end else
  21. $mysqli->close();
  22. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement