Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Suppose you wanted all the results from all
- * the rows in the table and ordering the results
- * by the id table (if you had one). You would
- * do something like this:
- **/
- $query = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC")
- // we want all the results, so let's make a loop
- while ($row = mysql_fetch_array($query)){
- // Some imaginary rows
- $id = $row[id];
- $user = $row[username];
- $pass = $row[pass];
- // Return the output to the page
- echo $id.' '.$user.' '.$pass."\n";
- }
- /**
- * Ok. That was easy enough. But let's suppose you
- * don't want to return all the results in the table,
- * and you only want one result? That's fair enough.
- * We'll try something like this with the WHERE condition :)
- **/
- // Grabbing the username where the id equals 1.
- $query = mysql_query("SELECT `username` FROM `table` WHERE `id`='1'");
- // Now, to turn that mysql resource into an actual result,
- // we use the mysql_result function.
- $result = mysql_result($query, 0);
- // Return the result on the page.
- echo $result;
- /**
- * It is best to use WHERE conditions if possible, because it
- * narrows down your results to the exact thing. (Absolutely
- * essential on a multi-user system). Here is what I use for
- * extracting settings from the `settings` table that have
- * two columns name (id, value).
- * Let's assume that I have the values ('template', 'default')
- * in the `settings` table. Here's a mysql query that returns
- * the value of the setting named 'template' :)
- **/
- $query = mysql_query("SELECT `value` FROM `settings` WHERE `id`='template'");
- $template = mysql_result($query, 0);
- // This would return the result of 'default'
- echo $template;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement