Jump to content

mysql array returns nothing?


steelth

Recommended Posts

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';

?>

 

Link to comment
https://forums.phpfreaks.com/topic/3200-mysql-array-returns-nothing/
Share on other sites

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.

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';

?>

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.

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).

[!--quoteo(post=336760:date=Jan 15 2006, 02:39 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Jan 15 2006, 02:39 PM) 336760[/snapback][/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).

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.

[!--quoteo(post=336765:date=Jan 15 2006, 02:48 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Jan 15 2006, 02:48 PM) 336765[/snapback][/div][div class=\'quotemain\'][!--quotec--]

again or is evaluated before =, so they will still die if the aforementioned conditions applies.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.