Retrieving Query Data [SOLVED]
Posted: Fri Jul 04, 2008 4:56 pm
Ok, here's tough question and I'm getting a strange results. I have this book I'm using (PHP and MySQL for Dynamic Websites) and I'm reading it trying to figure out how I can retrieve data from a query and display it on the page. Ok so here is what I'm looking at. I won't give you the url because I'll probably be messing around with it while waiting for replies.

In the left menu I'm going to have the top 10 most viewed guides left aligned and with a hyperlink to the page (which hasn't been attempted in the code I will show you. I'm trying to get it to display first). Right aligned to the right of it (in another td or div) I will display the amount of views. Right now just to display what I want it to look like It's just using <ul><li> tags. Now my book is telling me to go by this script that I followed as close as possible (but made changes to adapt to my needs).
To narrow it down, the mysql_connect, mysql_select_db, and mysql_query all work fine because I've tested it with another script and it works. Furthermore, the query itself works because when I use the PhpMyAdmin query the results display fine-

So I think It's safe to say that the problem is this last chunk of code-
And when I enter this code in (I've tried the rest of the code but excluded this and the page wasn't altered) all the rest of the code from here on out fails.

Don't worry about that chunk of text that is now in the left menu. That's just the forced ad that sits at the bottom (that I shrunk down). But everything from there over disappeared (because that come relatively first in the index.php file). So my question is, How can I retrieve data and display it? Why won't my code work?
Thanks in advanced

In the left menu I'm going to have the top 10 most viewed guides left aligned and with a hyperlink to the page (which hasn't been attempted in the code I will show you. I'm trying to get it to display first). Right aligned to the right of it (in another td or div) I will display the amount of views. Right now just to display what I want it to look like It's just using <ul><li> tags. Now my book is telling me to go by this script that I followed as close as possible (but made changes to adapt to my needs).
Code: Select all
<?php
$host = 'localhost';
$user = 'groog_admin';
$pass = 'password';
$query = "SELECT guide_name AS name, guide_views AS views FROM guides_table ORDER BY guide_views ASC LIMIT 10";
$connect = @mysql_connect($host, $user, $pass) or die('Error connecting to MySQL!');
@mysql_select_db(groog_guides) or die('Error connecting to database!');
$result = @mysql_query($query);
if ($result){
echo '<tr><td align="left">Name</td><td align="right">Views</td></tr>";
while ($row = $mysql_fetch_array($result, MYSQL_ASSOC))
{
echo '<tr><td align="left">' . $row['name'] . '</td><td align="right">' . $row['views'] . '</td></tr>';
}
mysql_free_result($result);
mysql_end();
?>
So I think It's safe to say that the problem is this last chunk of code-
Code: Select all
if ($result){
echo '<tr><td align="left">Name</td><td align="right">Views</td></tr>";
while ($row = $mysql_fetch_array($result, MYSQL_ASSOC))
{
echo '<tr><td align="left">' . $row['name'] . '</td><td align="right">' . $row['views'] . '</td></tr>';
}
Don't worry about that chunk of text that is now in the left menu. That's just the forced ad that sits at the bottom (that I shrunk down). But everything from there over disappeared (because that come relatively first in the index.php file). So my question is, How can I retrieve data and display it? Why won't my code work?
Thanks in advanced