David David - 5 months ago 13
SQL Question

mysql_query returns a row, but mysql_fetch_array does not do anything

I have a strange problem this time. I've got a masking database setup that takes in a url and gives it a string mask so that I can redirect users to another site (direct download) by giving them a link to my site's redirection engine.

I'm having a problem in the administrator side (where I put in the urls) where my mysql_fetch_array command just doesn't run. I know the query is returning at least one row because when I run the following code:

$query = "SELECT * FROM `urls` WHERE `in`='$lks[$i]' ORDER BY `id` DESC LIMIT 1";

$r = mysql_query($query);

if(mysql_num_rows($r) < 1)
echo "Less than 1";
else
echo "At least 1";


it returns "At least 1", but when it gets down to the mysql_fetch_array while statement, nothing happens. It's like it just ignores it completely.

CODE:

$query = "SELECT * FROM `urls` WHERE `in`='$lks[$i]' ORDER BY `id` DESC LIMIT 1";

$r = mysql_query($query);

while($me = mysql_fetch_array($r))
{
echo "Blah";
}


Nothing is echoed by this code. What's wrong?

By the way, I know that I am connected to the database and everything because I run other commands very similar to this before this one and they all execute without any problems.

Also this command is in a for loop (I don't know why this would be a problem because I have done this many times before and never had any problems).

Answer

try echoing mysql_error(); I'm thinking it might be that you put your column and table names in quotes.

$query = "SELECT * FROM urls WHERE in='$lks[$i]' ORDER BY id DESC LIMIT 1";
$r = mysql_query($query);
if($r)
{
$rowCount = mysql_num_rows($r);
if($rowCount == 1)
{
echo "1 row";
}
}
else
{
echo mysql_error(); 
}

if this doesn't work there must be something wrong with your array. :/

Comments