If someone has a simple script snippet that runs a query and outputs it to a web page, please post it. I got the mySQL query part right, I just can't figure out why PHP won't output it to the browser. Or, if you have a link. Most of the examples I am finding online have a variable being inserted into the query via a form with the POST method. Much appreciated!
$query = mysql_query("SELECT * from TABLE") or die(mysql_error()); if(mysql_num_rows($query) > 0) { while($a=mysql_fetch_array($query)) { echo $a['ID']; echo $a['name']; } } else { echo 'No Results Found'; } Code (markup):
There is no input from a form, just a standard query and display: <?php $query = "SELECT * FROM SEARCH_TERMS ORDER BY SEARCH_TIME DESC LIMIT 30"; $result = mysql_query($query); while($arr = mysql_fetch_assoc($result)){ extract($arr); echo "$result "; } ?> PHP: I'm a noob but I thought I might have had it....
Try this: <?php $query = "SELECT * FROM SEARCH_TERMS ORDER BY SEARCH_TIME DESC LIMIT 30"; $result = mysql_query($query); while($arr = mysql_fetch_assoc($result)){ echo '<pre>'; print_r($result); echo '</pre>'; $outputted = true; } if (!$outputted){ echo " - No Results Found - "; } ?> PHP:
Try that, adjusted the line. Sorry! <?php $query = "SELECT * FROM SEARCH_TERMS ORDER BY SEARCH_TIME DESC LIMIT 30"; $result = mysql_query($query); while($arr = mysql_fetch_assoc($result)){ echo '<pre>'; print_r($arr); // Adjusted line echo '</pre>'; $outputted = true; } if (!$outputted){ echo " - No Results Found - "; } ?> Code (markup):
Yeah, I tried that too but go this as an output to the browser: Array ( [id] => 18 [search_keyword] => podcast [search_time] => 2008-01-15 12:01:22 ) Array ( [id] => 17 [search_keyword] => ipod [search_time] => 2008-01-15 11:54:10 ) Array ( [id] => 16 [search_keyword] => dvd [search_time] => 2008-01-15 10:08:16 ) Array ( [id] => 15 [search_keyword] => phone [search_time] => 2008-01-15 09:59:57 ) Array ( [id] => 14 [search_keyword] => mysql [search_time] => 2008-01-14 21:58:00 ) Code (markup): I can't figure this one out honestly.
Thanks for your assistance everyone... I figured it out, it was in my query (which I originally thought was correct) <?php $query = "SELECT search_keyword FROM search_terms ORDER BY search_time DESC LIMIT 6"; $result = mysql_query($query); while($arr = mysql_fetch_assoc($result)){ extract($arr); echo $arr['search_keyword'] ; } ?> PHP:
I wasn't sure what you wanted from the output, so I printed the entire array so you can see everything in that row. Sorry Jay