|
-
May 12th, 2005, 09:36 AM
#1
Thread Starter
Frenzied Member
results are empty for query... why?
I've tried using the following code to access one record in my database table...
PHP Code:
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
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 12th, 2005, 10:20 AM
#2
Re: results are empty for query... why?
CHange this:
PHP Code:
$sql = " SELECT * FROM myTable ";
$sql .= " WHERE id=$fc ";
To this:
PHP Code:
$sql = "SELECT * FROM `myTable` WHERE `id` = '$fc'";
And it should then work 
Cheers,
RyanJ
-
May 12th, 2005, 10:30 AM
#3
Thread Starter
Frenzied Member
Re: results are empty for query... why?
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
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 12th, 2005, 11:04 AM
#4
Re: results are empty for query... why?
Ah, I think i have found it:
PHP Code:
if (mysql_num_rows() == 0)
Should be:
PHP Code:
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
-
May 12th, 2005, 11:04 AM
#5
Thread Starter
Frenzied Member
Re: results are empty for query... why?
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
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
May 12th, 2005, 08:22 PM
#6
Member
Re: results are empty for query... why?
They wont be able to recognize the variables, unless they are global. Take a look here variable scope
-
May 13th, 2005, 01:23 AM
#7
Re: results are empty for query... why?
 Originally Posted by squirrelly1
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:
PHP Code:
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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|