Page 1 of 1

while, mysql_fetch_row (someings wrong)

Posted: Sun Aug 26, 2007 9:31 am
by hgmmy
What's wrong with this code?

Code: Select all

<?php 

$list_categories2 = mysql_query("SELECT * FROM wp_categories");

while ($row3 = mysql_fetch_row($list_categories2)) {
echo "<li><a href='$row3[3]'>$row3[1]</a></li>";
}

?>
I have almost that exact same thing in another spot, with the only differences being $row3 is $row and $lst_categories is $list_pages, and it works fine. What it should be doing is accessing a data base were there is a list of links to different categories, but it's changing the value of $row3 in the echo section so that it's getting the wrong data.

Just so you know I'm still new at php/mysql.

Posted: Sun Aug 26, 2007 9:36 am
by VladSun
[s]It is maybe the 5th thread about this problem this week ... You can't concatenate array items in the way you do it - you have to use surrounding {} or using the "." operator.
E.g.
"bla-bla {$array[$ndex]} bla-bla"
or
"bla-bla ".$array[$index]." bla-bla"[/s]

Posted: Sun Aug 26, 2007 10:06 am
by feyd
VladSun wrote:It is maybe the 5th thread about this problem this week ... You can't concatenate array items in the way you do it - you have to use surrounding {} or using the "." operator.
E.g.
"bla-bla {$array['key']} bla-bla"
or
"bla-bla ".$array['key']." bla-bla"
Unfortunately, this shouldn't have any baring on the problems hgmmy is experiencing.

Posted: Sun Aug 26, 2007 10:16 am
by VladSun
[s]Given that information, it is only question ("What's wrong with this code? ") answerable and the only answer is what I've written :)
If I didn't understand the question then it is due to the lack of information or because of my English.
If it is the last one, let me been excused, please :)[/s]

Posted: Sun Aug 26, 2007 10:21 am
by feyd
It's actually not an answer as it's technically not a problem. It's perfectly legal code.

Posted: Sun Aug 26, 2007 10:29 am
by VladSun
Oh, it works indeed :)
I'd never know that - I'd always put {} arround array items :)

Thanks, fyed! Every day you teach me somtehing new :)

Posted: Sun Aug 26, 2007 1:00 pm
by califdon
hgmmy wrote:What's wrong with this code?

Code: Select all

<?php 

$list_categories2 = mysql_query("SELECT * FROM wp_categories");

while ($row3 = mysql_fetch_row($list_categories2)) {
echo "<li><a href='$row3[3]'>$row3[1]</a></li>";
}

?>
I have almost that exact same thing in another spot, with the only differences being $row3 is $row and $lst_categories is $list_pages, and it works fine. What it should be doing is accessing a data base were there is a list of links to different categories, but it's changing the value of $row3 in the echo section so that it's getting the wrong data.

Just so you know I'm still new at php/mysql.
I don't see anything incorrect in your code, although it would be easier for you and others to read and debug your code if you adopted the practice of using mysql_fetch_assoc() instead of mysql_fetch_row(), which returns an array with the associated field names so that you can more easily use the names, something like this:

Code: Select all

$list_categories2 = mysql_query("SELECT * FROM wp_categories") or die("Failed: ".mysql_error());
while ($row3 = mysql_fetch_assoc($list_categories2)) {
echo "<li><a href="$row3['url']">$row3['name']</a></li>";
But, back to your original issue, especially since the nearly identical code is working elsewhere, you need to look at exactly what your SQL query looks like, as you send it to the database. You can easily do this by adding the or die(mysql_error()) clause to your query statement, as shown above. If the query fails, you will be shown the MySQL error message and the script will end. If you do this routinely, you will be able to catch many errors like a variable not being assigned a value. On the other hand, if the query doesn't fail, you will need to add a temporary debug line something like:

Code: Select all

echo $sql;
in order to see the query. That assumes that you first assigned the SQL string to a variable like $sql and used it in your mysql_query() statement.

Posted: Sun Aug 26, 2007 6:32 pm
by hgmmy
Thanks Califdon, I changed to what you suggested and put ". ." around the $row[] (arrays right?) parts. and it seem to be working right now, and if it quits working I'll let ya'll know.