1

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

3
  • 3
    please don't use mysql_* functions, it's deprecated (see red box) and vulnerable to sql-injection. Use PDO or MySQLi. Commented Aug 18, 2012 at 1:52
  • do a var_dump($me); also, echo out your $query variable to make sure it contains what you think it does. I'm not sure if array indexing inside "" will work properly. Commented Aug 18, 2012 at 1:57
  • where do I do the var_dump? After the while statement terminates, $me returns bool(false)? Commented Aug 18, 2012 at 2:02

3 Answers 3

1

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. :/

Sign up to request clarification or add additional context in comments.

8 Comments

echo mysql_error(); returned nothing. Changing 'in' (hash quotes, not apostrophes) to in gave me an invalid resource error in the mysql_fetch_array() line.
Same thing. if($r) does run, but mysql_error returns nothing.
I also recommend NOT quoting column names. AND on most servers they are case sensitive.
lol Sorry for all the edits guys :( I can't figure this stupid indention out.
Same thing, the resource is valid, it has returned one row, but it will not run mysql_fetch_array($r)
|
0

This is wrong:

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

should be:

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

But like stated earlier, use PDO.

1 Comment

Adding that still did nothing. I'm still getting a valid result with 1 row in it, but the mysql_fetch_array command will not run.
0

You are checking for less then one row kindly echo mysql_num_rows($r) and check how many rows were returned.

or use die(mysql_error()) alongwith mysql_query to check for error.

4 Comments

echo mysql_num_rows($r); returns 1
Then it is impossible that it will not enter the while loop. Check for any syntax error in your code.
Why do you think I'm so confused. I even took it out of the while loop and hard-coded a url into the query so that I could see if that was the problem and the same thing happens, every time.
use print_r(mysql_fetch_array($r)) to check the result... whats the output. (Not in while loop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.