Jump to content


Photo

mysql array returns nothing?


  • Please log in to reply
6 replies to this topic

#1 steelth

steelth
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 January 2006 - 09:27 PM

My problem starts in the while() loop. $row = mysql_fetch_assoc($result) returns no value even though i have records in my database.

Same with:

$id = $row['id'] or die ("Failed");
$gold = $row['gold'] or die ("Failed");
$title = $row['title'] or die ("Failed");
$description = $row['description'] or die ("Failed");
$link = $row['link'] or die ("Failed");

They return nothing :(




<?php
include '../config2.php';
include '../dbconnect.php';

$sql = 'SELECT * FROM downloads ORDER BY title ASC';
$result = mysql_query($sql) or die ("Table Selection Failed");


if(mysql_fetch_array($result) == TRUE)
{
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'] or die ("Failed");
$gold = $row['gold'] or die ("Failed");
$title = $row['title'] or die ("Failed");
$description = $row['description'] or die ("Failed");
$link = $row['link'] or die ("Failed");

echo '<table width="380" border="1"><tr><td><div align="center">';
if($gold == true)
{
echo '*';
}
echo '<a href="';
echo $link;
echo '" target="_blank" class="';
if($gold == true)
{
echo 'fontsize12 colorFFB200" id="';
}
else
{
echo'color8EA0C1 fontsize12" id="';
}
echo $id;
echo '">';
echo $title;
echo '</a>';
if($gold == true)
{
echo '*';
}
echo '</div></td></tr></table><table width="380" border="0"><tr><td><div align="center">';
echo $description;
echo '</div></td></tr></table><div align="center"><br></div>';
}
}
else
{
echo '<div align="center">There are no Links in this category.</div>';
}
include '../dbclose.php';
?>


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 14 January 2006 - 10:37 PM

First, what's with the die() statements on assignment operations? That'll never work -- how can such an assignment EVER fail? And what would it mean if it did?!?!

Second, by calling mysql_fetch_array(), you've advancing the row pointer _before_ the start of the while loop. You'd be better simply calling mysql_fetch_assoc() directly (since I'm pretty sure it simply returns FALSE if there are no rows) or check the number of records with mysql_num_rows() -- which would also be a good test of the query itself.

Good luck -- post the results of your investigation here, and we'll take it from there.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 steelth

steelth
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 January 2006 - 04:05 AM

Still returns no rows.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] <?php

include '../config2.php';
include '../dbconnect.php';

$sql = 'SELECT * FROM downloads ORDER BY title ASC';
$result = mysql_query($sql) or die ("Table Selection Failed");


if(mysql_num_rows() >0)
{
while($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
$gold = $row['gold'];
$title = $row['title'];
$description = $row['description'];
$link = $row['link'];

echo '<table width="380" border="1"><tr><td><div align="center">';
if($gold == true)
{
echo '*';
}
echo '<a href="'.$link.'" target="_blank" class="';
if($gold == true)
{
echo 'fontsize12 colorFFB200" id="';
}
else
{
echo'color8EA0C1 fontsize12" id="';
}
echo $id.'">'.$title.'</a>';
if($gold == true)
{
echo '*';
}
echo '</div></td></tr></table><table width="380" border="0"><tr><td><div align="center">'.$description.'</div></td></tr></table><div align="center"><br></div>';
}
}
else
{
echo '<div align="center">There are no Links in this category.</div>';
}
include '../dbclose.php';
?>
[/quote]

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 January 2006 - 05:13 AM

How very uninformative. First, you need to pass the resource to mysql_num_rows(). Second, what is the return value of this function? What is the value of $result? Assuming you have records in your table, you should be retrieving a non-zero value.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 15 January 2006 - 07:39 PM

fenway, the die() statement is associated with mysql_query() in this case. if the query contains error (so mysql_query() returns false), it will die. It has nothing to do with the assignment. In other words, "or" is evaluated before assignment (or have precedence over assignment).
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 January 2006 - 07:46 PM

[!--quoteo(post=336760:date=Jan 15 2006, 02:39 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Jan 15 2006, 02:39 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
fenway, the die() statement is associated with mysql_query() in this case. if the query contains error (so mysql_query() returns false), it will die. It has nothing to do with the assignment. In other words, "or" is evaluated before assignment (or have precedence over assignment).
[/quote]
I was referring to the other 5 die() statements in the _original_ post that will have no effect whatsoever. And "or have precedence over assignment"? Maybe, but assignment can never fail.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 January 2006 - 08:48 PM

[!--quoteo(post=336765:date=Jan 15 2006, 02:48 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Jan 15 2006, 02:48 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
again or is evaluated before =, so they will still die if the aforementioned conditions applies.
[/quote]

That's simply not true according to [a href=\"http://phpbuilder.com/manual/en/language.operators.php#language.operators.precedence\" target=\"_blank\"]the PHP operator precedence table[/a] -- "or" has the lowest precedence.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users