Advertisement
Guest User

Untitled

a guest
May 10th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Suppose you wanted all the results from all
  5.  * the rows in the table and ordering the results
  6.  * by the id table (if you had one). You would
  7.  * do something like this:
  8.  **/
  9.  
  10. $query = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC")
  11.  
  12. // we want all the results, so let's make a loop
  13. while ($row = mysql_fetch_array($query)){
  14.     // Some imaginary rows
  15.     $id = $row[id];
  16.     $user = $row[username];
  17.     $pass = $row[pass];
  18.  
  19.     // Return the output to the page
  20.     echo $id.' '.$user.' '.$pass."\n";
  21. }
  22.  
  23.  
  24. /**
  25.  * Ok. That was easy enough. But let's suppose you
  26.  * don't want to return all the results in the table,
  27.  * and you only want one result? That's fair enough.
  28.  * We'll try something like this with the WHERE condition :)
  29.  **/
  30.  
  31. // Grabbing the username where the id equals 1.
  32. $query = mysql_query("SELECT `username` FROM `table` WHERE `id`='1'");
  33.  
  34. // Now, to turn that mysql resource into an actual result,
  35. // we use the mysql_result function.
  36. $result = mysql_result($query, 0);
  37.  
  38. // Return the result on the page.
  39. echo $result;
  40.  
  41. /**
  42.  * It is best to use WHERE conditions if possible, because it
  43.  * narrows down your results to the exact thing. (Absolutely
  44.  * essential on a multi-user system). Here is what I use for
  45.  * extracting settings from the `settings` table that have
  46.  * two columns name (id, value).
  47.  * Let's assume that I have the values ('template', 'default')
  48.  * in the `settings` table. Here's a mysql query that returns
  49.  * the value of the setting named 'template' :)
  50.  **/
  51.  
  52. $query = mysql_query("SELECT `value` FROM `settings` WHERE `id`='template'");
  53. $template = mysql_result($query, 0);
  54.  
  55. // This would return the result of 'default'
  56. echo $template;
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement