PDA

Click to See Complete Forum and Search --> : results are empty for query... why?


squirrelly1
May 12th, 2005, 09:36 AM
I've tried using the following code to access one record in my database table...


function downloadfile($fc)
{
print "hmmm<br>";

$sql = " SELECT * FROM myTable ";
$sql .= " WHERE id=$fc ";

print "$sql<br>";

$rs = mysql_query($sql, $cid);
if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }

if (mysql_num_rows() == 0)
{
print "Nothing in the recordset.<br>";
} else {
print "Something in the recordset.<br>";
}

while ($row = mysql_fetch_array($rs))
{
print "hmm";
//$fileloc = $row['fileloc'];
//print "File Location: $fileloc";
//header("Location: " . $fileloc);
}
print "After while loop.<br>";
exit(0);
}


it returns the following...

hmmm
SELECT * FROM myTable WHERE id=59
Nothing in the recordset.
After while loop.


yet when I use this exact same query directly in MySQL it returns the appropriate record!

any ideas on why this is messing up like this?

Squirrelly1

sciguyryan
May 12th, 2005, 10:20 AM
CHange this:


$sql = " SELECT * FROM myTable ";
$sql .= " WHERE id=$fc ";


To this:

$sql = "SELECT * FROM `myTable` WHERE `id` = '$fc'";


And it should then work :)

Cheers,

RyanJ

squirrelly1
May 12th, 2005, 10:30 AM
The SQL statement was not the problem... that's what I was trying to express when I said that I checked the SQL statement directly in MySQL... My error trap would have detected if it were the problem...

still not working with ur modification, thanks though...

Squirrelly1

sciguyryan
May 12th, 2005, 11:04 AM
Ah, I think i have found it:


if (mysql_num_rows() == 0)


Should be:


if (mysql_num_rows($rs) == 0)


From the PHP Manual:


mysql_num_rows
(PHP 3, PHP 4 , PHP 5)

mysql_num_rows -- Get number of rows in result
Description
int mysql_num_rows ( resource result )

Retrieves the number of rows from a result set. This command is only valid for SELECT statements. To retrieve the number of rows affected by a INSERT, UPDATE, or DELETE query, use mysql_affected_rows().

Parameters


result
The result resource that is being evaluated. This result comes from a call to mysql_query().

Return Values
The number of rows in a result set on success, or FALSE on failure.



Cheers,

RyanJ

squirrelly1
May 12th, 2005, 11:04 AM
I think I figured out the problem, but I'm not sure how to solve it...

The code I listed is inside of a function within my php script. For some reason, the function is not able to see the $cid or any of the other variables that were set outside of the function.


How can I fix this or see if it really is the problem?

squirrelly1

Brandoe85
May 12th, 2005, 08:22 PM
They wont be able to recognize the variables, unless they are global. Take a look here variable scope (http://us2.php.net/variables.scope)

visualAd
May 13th, 2005, 01:23 AM
I think I figured out the problem, but I'm not sure how to solve it...

The code I listed is inside of a function within my php script. For some reason, the function is not able to see the $cid or any of the other variables that were set outside of the function.


How can I fix this or see if it really is the problem?

squirrelly1
You can either use a global declaration or access it directly through the $BLOBALS array.

You should test the return type of mysql_query() to check whether the query has failed:

function downloadfile($fc)
{
global $cid;

print "hmmm<br>";

$sql = " SELECT * FROM myTable ";
$sql .= " WHERE id=$fc ";

print "$sql<br>";

if (! $rs = mysql_query($sql, $cid)) {
print "Database Error: $sql " . mysql_error();
return;
}

if (mysql_num_rows($rs) == 0)
{
print "Nothing in the recordset.<br>";
} else {
print "Something in the recordset.<br>";
}

while ($row = mysql_fetch_array($rs))
{
print "hmm";
//$fileloc = $row['fileloc'];
//print "File Location: $fileloc";
//header("Location: " . $fileloc);
}
print "After while loop.<br>";
exit(0);
}