-5

Ok then guys, this is embarassing. I am getting this PHP error, already linted, checked code and don't get the answer. Any suggestion?

**Warning**: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in file.php on line **, heres the complete code

<?
session_start();

include 'keys.php';

include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
?>

As additional data, it pushes data correctly to databases but interrupts the following part of the code.

UPDATE: The problem was in the "require" files, not in the code I posted, I was using this code: while($results = mysql_fetch_array($results)) { in where I repeated 2 $results

Luis
  • 1,067
  • 1
  • 14
  • 25
  • 4
    Welcome to Stack Overflow! You are not doing any error checking in your query, so it's no wonder it breaks when the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Mar 03 '12 at 00:38
  • possible duplicate of ["mysql_fetch_assoc()" error when data in mysql field is changed](http://stackoverflow.com/questions/9431230/mysql-fetch-assoc-error-when-data-in-mysql-field-is-changed) – Pekka Mar 03 '12 at 00:39
  • 1
    This is getting out of hand... Luis, when you typed that title every single "Questions with similar titles" was a duplicate. – Mike B Mar 03 '12 at 00:39
  • Not sure, but you could try to escape the ' arond $username and $app: `mysql_query("SELECT * FROM `users` WHERE `user`=\'$username\' AND `authorized`=\'$app\'"); ` – Bjørne Malmanger Mar 03 '12 at 00:39
  • Hey, please don't bother. If I posted it here, it's because I haven't found any fix, I **ALREADY** used ALL the error reporting and it doesn't work! – Luis Mar 03 '12 at 00:43
  • 2
    @Luis When you entered the question title, you had thousands and thousands of identical questions in the suggestions box, all of which deal with *this exact issue.* Had you looked closely at one of them (or looked at the examples in the PHP manual) you would already be on the way to solving this – Pekka Mar 03 '12 at 00:45
  • @Pekka I know, but I just doesn't understand them, maybe (as I told on another comment) is not the best day to code. I need this code fixed, I already tried almost everything. Thanks! – Luis Mar 03 '12 at 00:48
  • Also, the strangest thing is that on 41 line I don't use mysql_fetch_array() – Luis Mar 03 '12 at 00:49
  • 1
    What does `mysql_error()` tell you? (And I see a fetch_array() call there. Can you confirm it is the correct one? Which is line 41?) – Pekka Mar 03 '12 at 00:49
  • It says line 55, mysql_error doesn't tell something, that's the strangest thing. Thanks @pekka – Luis Mar 03 '12 at 00:59
  • Duplicate everywhere! No, seriously... look at the "Related" sidebar -> – Shoe Mar 03 '12 at 01:12
  • @Luis It's not strange at all. Somewhere you're calling `mysql_fetch_array()` and passing in a boolean value instead of a mysql resource. Find where this (The error points out the line but for some reason you refuse to tell us what line that is) is and debug back to where the boolean is coming from. – Mike B Mar 03 '12 at 01:18
  • 3
    I'd love to know who found this question useful. – Mike B Mar 03 '12 at 01:20
  • The problem was in the inserted codes, thanks to everybody – Luis Mar 03 '12 at 01:29
  • @Luis The problem was IN the code? Where else would it be?! Please tell us what you did to fix it that was different from all the duplicates that were pointed out to you. – Mike B Mar 03 '12 at 21:37
  • possible duplicate of [Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result](http://stackoverflow.com/questions/795746/warning-mysql-fetch-array-supplied-argument-is-not-a-valid-mysql-result) – DCoder Aug 09 '12 at 19:21

2 Answers2

2

Errors occur here

$count_users = mysql_query("SELECT * FROM `users` WHERE `user`='$username' AND `authorized`='$app'");

$results_count = mysql_num_rows($count_users);


 ****=> $count_users is not valid resource id so** $results_count returns some value which is not 0**
        **=> therefore not going into this if statement**

        if ($results_count == 0) 
        {

        }
        else  **=> instead going to this else statement but $count_users is not valid resource id so mysql_fetch_array throws error**       
        { 
            while($count_results = mysql_fetch_array($count_users)) {$tokeni = $count_results["token"];}
            mysql_query("UPDATE `users` SET `date`=CURDATE(), `time`=CURTIME(), `token`='$final_token', `secret`='$final_secret' WHERE `token`='$tokeni'") or die(mysql_error());   
        }
Hieu Van Mach
  • 673
  • 3
  • 8
  • I didn't understand you answer, can you edit it and make it cleaner? Thanks! – Luis Mar 03 '12 at 00:58
  • Sorry for my uncleaned answer as I am new to this but what I mean is the $count_users is not valid resource id and then it does not satisfy your statement if ($results_count == 0) so it goes into else statement which it is assumed that there is at least one row returned from db but again $count_users is not valid resource id so mysql_fetch_array($count_users) throws error mysql_fetch_array(): supplied argument is not a valid MySQL result resource – Hieu Van Mach Mar 03 '12 at 01:31
0

You're assuming that mysql_query() with a SELECT statement always returns a resultset resource. This is not the case. Quote from mysql_query() documentation:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Thus, in case of error you end up passing a boolean FALSE value to mysql_fetch_array() instead of a resultset. This is why one should check whether the value returned by mysql_query() is FALSE before passing it to other functions as a resultset.

UPDATE: Apparently the code we were shown was different from the one actually causing the problem. The problem was caused by passing a return value from mysql_fetch_array() back to it on a second call:

while($results = mysql_fetch_array($results))

Either way, passing a value which isn't a resultset to mysql_fetch_array() was the cause of the issue exactly as the error message said:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in file.php on line

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92