Web Hosting Talk







View Full Version : Getting errors PHP


Lippy
12-02-2002, 01:51 AM
I am getting errors in the following lines. Any help would be greatly appreciated.


$row = mysql_fetch_array($result);
if(mysql_num_rows($result) >0)


Here is the full php block.


<?

// Start "leftside news"
$query4 = "SELECT *, time AS timeorder, date_format(time, '%Y-%m-%d') AS today, date_format(time, '%a %c-%d-%Y %T')
AS time FROM reviews ORDER BY timeorder DESC LIMIT 3, $numsideitems";
$result = mysql_query($query4);
if (!$result)
{
echo( "Unable to locate the query results at this time." );
}
$row = mysql_fetch_array($result);
if(mysql_num_rows($result) >0)
{
do
{
$query5 = "SELECT * FROM authors WHERE id = '" . $row["author"] . "'";
$result2 = mysql_query($query5);

$row2 = mysql_fetch_array($result2);
echo("<a href=\"reviews.php?id=" . $row["id"] . "\">" . $row["title"] . "</a><BR><BR>");

}while( $row = mysql_fetch_array($result) );
}

?>

:: paVel ::
12-02-2002, 04:06 AM
Can you please provide some comments on what this script does.

Regards, Pavel

daveman
12-02-2002, 04:17 AM
What error are you getting?

sylow
12-02-2002, 07:29 AM
get num of rows first before fecthing an array. If there is no result set it returns error most probably.

MHO

Rich2k
12-02-2002, 08:39 AM
I'd love to know who is documenting checking a mysql array with a mysql_num_rows

You only need to do

if ($result) {
}
else {
}

And you can do it all without having an extra php command. It just seems to make more sense to me.

Don't get me wrong checking the num of rows is find if you want to actually compare the number of returned results but it's pointless for checking against 0 alone.

jtrovato
12-02-2002, 08:44 AM
<?
// Start "leftside news"
$query4 = "SELECT *, time AS timeorder, date_format(time, '%Y-%m-%d') AS today, date_format(time, '%a %c-%d-%Y %T')
AS time FROM reviews ORDER BY timeorder DESC LIMIT 3, $numsideitems";

$result = mysql_query($query4); // If first query doesn't come back with a result then give error message
if (!$result)
echo( "Unable to locate the query results at this time." );
else // If there is a result display it
{
$row = mysql_fetch_array($result); // Hopefully this will only return 1 result, the code is written for only "1" result to be returned
$num = mysql_num_rows($result); // The way your orginal code is written, this number should only be "1"
for ($i=0;$i<$num;$i++)
{
$query5 = "SELECT * FROM authors WHERE id = '".$row["author"]."'";
$result2 = mysql_query($query5);
$row2 = mysql_fetch_array($result2);
echo("<a href=\"reviews.php?id=" . $row["id"] . "\">" . $row["title"] . "</a><BR><BR>");
while( $row = mysql_fetch_array($result) );
}
}
?>


The way the code it written, you have logical errors all over the place. Maybe if you tell us what the results should be and that the queries are correct according to your database structure.

I tried re-writting the code, but it doesn't make sense why you would have a query within a loop? Maybe you should only query the database once, and then loop through the result.

Lippy
12-02-2002, 06:40 PM
The result should be the 4th 5th and 6th from last entry into the database, it used to work and works on my local setup apache machine. I am starting to think its more a server error than a scripting error.