0

Ok, so I know the question about 'why am I getting this warning with mysql_fetch_array...' has been asked several times, my problem is all the accepted answers state that the reasons the server is spitting this warning out is because the query itself is incorrect...this is not the case with me.

Here is the code below:

$dbhost = "host";
$dbuser = "user";
$dbpass = "pass";
$dbname= "db";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die ('<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Failed.php?dberror=1">');    

$token = mysql_escape_string($_GET['token']); 

$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_array($result)) {
   do stuff...
}

Everything within the 'while' statement is being executed just fine - it makes some changes to the DB which I can validate is happening. More importantly, the query never spits out any error details. I've tried testing for cases where $result===false and asking for error info but it won't return anything then either. From what I can tell, the query string is fine and is not failing.

What am I doing wrong here? Could there any other reason why PHP doesn't like my While parameters other than the SQL statement is bad (which again, I'm convinced it's not bad)?

Also, I know I should be using mysqli/PDO....I plan to switch over to that in the near future, but I'm pulling my hair out just trying to make this work and I have no idea why it won't. It's more of a personal thing at this point...

Thanks for your help, and let me know if you need any additional info. (PHP Version 5.3)

Here is an echo of the query string ($query):

  SELECT * FROM newuser WHERE token='6e194f2db1223015f404e92d35265a1b'

And here is a var_dump of the query results ($result): resource(3) of type (mysql result)

echo_Me
  • 37,078
  • 5
  • 58
  • 78
Caderade
  • 65
  • 2
  • 2
  • 8
  • So `echo mysql_error()` below `mysql_query()` doesn't output anything? – Michael Mar 30 '13 at 22:00
  • That usually means your query is not correct. – y-- Mar 30 '13 at 22:00
  • the title `mysql_fetch_array` and the code `mysql_fetch_assoc` please write your code properly. – echo_Me Mar 30 '13 at 22:02
  • I understand what this mean, but I'm telling you, the query is right. Token is coming from a get variable. It's a hash - so just basic alphanumeric characters (no symbols). I perform a mysql_escape_string function on it, but since there's no special characters to escape, doesn't really matter – Caderade Mar 30 '13 at 22:04
  • @echo_me I apologize, I've tried both array and assoc, just to be sure. 'Array' was the original version I was using – Caderade Mar 30 '13 at 22:04
  • hello @Caderade can you please echo that echo "SELECT * FROM newuser WHERE token='$token'"; then try to var_dump($query);and tell us what did you get :) – Last Breath Mar 30 '13 at 22:11
  • So, echo the query string itself, and then do the vardump on the results? – Caderade Mar 30 '13 at 22:13
  • @LastBreath here are the results from echo: SELECT * FROM newuser WHERE token='6e194f2db1223015f404e92d35265a1b' AND here are the results from the var_dump: resource(3) of type (mysql result) – Caderade Mar 30 '13 at 22:20
  • Hello @Caderade : Sorry it was a typo i mean that you must try to echo $query; then var_dump($result) and tell us the result :) – Last Breath Mar 30 '13 at 22:21
  • Hey @Caderade What if you use this : echo mysql_num_rows($result); What did you got ? – Last Breath Mar 30 '13 at 22:22
  • ok cool, I added the echo/vardump in question above – Caderade Mar 30 '13 at 22:23
  • @MichaelRushton hey man, sorry I didn't respond to you. No, 'echo mysql_erro()' doesn't output anything below the query statement – Caderade Mar 30 '13 at 22:27
  • Hello @Caderade : You did not tell me if you put that : echo mysql_num_rows($result); what the result you get ? – Last Breath Mar 30 '13 at 22:29
  • @LastBreath hey, the mysql_num_rows function returns '1', which is good because it's only supposed to have 1 row :) – Caderade Mar 30 '13 at 22:30
  • @Caderade Please as i think now that the problem is not with this line you have posted as code but in another mysql_fetch_array inside your code so can you please tell me if their were any mysql_fetch_array inside your code else this one :) – Last Breath Mar 30 '13 at 22:42
  • _Everything within the 'while' statement is being executed just fine - it makes some changes to the DB which I can validate is happening_ Are you using `$result` inside this while loop? – Terje D. Mar 30 '13 at 22:43
  • @LastBreath No, there are only just some update queries, and session unset/destroy commands – Caderade Mar 30 '13 at 22:44
  • Look the error that is occured means that the parameter given for a mysql_fetch_array is a boolean and as i guess a boolean value may be got from an update query like if you update your user name then assign the query result to a variable then this will return 1 or 0 which is boolean and the warning that you get is somethin like this so can you please post more code if you can ? :) – Last Breath Mar 30 '13 at 22:47
  • @TerjeD. that was it!!! holy crap, I can't believe I did that. Thanks a ton. – Caderade Mar 30 '13 at 22:50
  • @LastBreath looks like you were going the same way with that one. Thanks for your help too. That was a stupid mistake on my part. – Caderade Mar 30 '13 at 22:51
  • @Caderade Look why you don't ignore the using of mysql_fetch_array in your case it is enough to you that the $result variable is a resource to proceed your process so you don't need it at all , either if you want to extract some data from the returned result ? :) – Last Breath Mar 30 '13 at 22:52
  • @LastBreath Do I not have to perform the mysql_fetch_array function to pull out the data? I didn't know of any other way to get the data from the result – Caderade Mar 30 '13 at 22:53
  • @Caderade first of all i told you to ignore the using of the mysql_fetch_array just in the case you don't need to fetch any result and in the case you are using this query just to validate the token of the user but if you want to fetch the row you have also one choice is to use the mysql_fetch_assoc($result); and that will give you the result :) – Last Breath Mar 30 '13 at 22:56
  • @LastBreath yeah I'm not just validating the token, I need to pull more data out of the row so I might just use the mysql_fetch_assoc instead. Thanks again for the help, it was awesome! – Caderade Mar 30 '13 at 23:05
  • @Caderade For nothing :) and i am glad that i drive you to the correct solution :) And don't hesitate always to ask me if you have any problem :) – Last Breath Mar 30 '13 at 23:06

4 Answers4

5
$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_array($result)) {
   do stuff...
}

If the die statement is not executed, $result is OK when you enter the while loop. The problem then is probably that you use $result for a query inside the loop as well, eventually leading to it being set to false.

Terje D.
  • 6,250
  • 1
  • 22
  • 30
2

So for now i can say that the problem is not the mysql_escape_string nether the using of mysql at all neither access privilege from user name and password and what i want to tell you is to test the $result and if it is a resource proceed with your while block like this

if(is_resource($result))
    {
        while($row = mysql_fetch_array($result))
            {//process your code here}
    }

and tell me if the code has been also executed :)

Last Breath
  • 530
  • 2
  • 12
1

The query is not correct according to mysql_. The error you're receiving is telling you that $result is boolean (false).

Where is $token coming from? You best stop using mysql_ functions and use a prepared statement and a bound parameter.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • What about it could be wrong? I can perform that very sql statement in the console through php myadmin and it's fine. BTW, I edited the code to show where token was coming from – Caderade Mar 30 '13 at 22:07
0

your escape is wrong try this

     $token = mysql_real_escape_string($_GET['token']);

instead of $token = mysql_escape_string($_GET['token']);

This extension is deprecated as of PHP 5.5.0, and will be removed in the future.

http://php.net/manual/en/function.mysql-real-escape-string.php

echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • I'll definitely try it, but can I ask why that would matter? – Caderade Mar 30 '13 at 22:08
  • yes its matter because its not defined your function `mysql_escape_string` , – echo_Me Mar 30 '13 at 22:10
  • right, I'll move to PDO eventually. Just don't have the time right now - this is easier for me to implement because I've used it elsewhere in my application – Caderade Mar 30 '13 at 22:12
  • actually, no haha. I got another warning which said: [function.mysql-real-escape-string]: Access denied for user '*username*'@'localhost' (using password: NO) – Caderade Mar 30 '13 at 22:16
  • Is that because of a PHP setting I guess? – Caderade Mar 30 '13 at 22:16
  • this its other question about the access for user , check your user password , maybe you are mayking `NO` in phpmyadmin . so now u dont have problem with the code you posted .check username and password and phpmyadmin seting – echo_Me Mar 30 '13 at 22:18
  • @echo_me There is also a [mysql_escape_string](http://php.net/manual/en/function.mysql-escape-string.php) function. – ba0708 Mar 30 '13 at 23:01