0

Possible Duplicate:
php warning mysql_fetch_assoc

I have a weird problem about my script. It returns always error for mysql_fetch_array or mysql_fetch_assoc. I have used mysql_fetch many times in my project and I checked for this error many times but I am blind about what is happening. Is there something wrong about my script?

My functions aim is learning the biggest value of specified mysql field.

Here is the function:

function nextIncrement($table,$field) {
    $sql = mysql_query("SELECT '$field' FROM '$table' ORDER BY '$field' DESC LIMIT 0,1");
    while($row = mysql_fetch_assoc($sql)) {
        $next = $row[$field];
    }
    $next = (int)$next;
    return $next;
}

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource ...

Community
  • 1
  • 1
aiternal
  • 1,080
  • 3
  • 16
  • 33

3 Answers3

1

Most likely, your mysql_query() returned false for some reason.

See the manual for a list of possible values that mysql_query() can return.

Do a echo mysql_error(); to see what's wrong.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Query works fine in sql editor but it returns error when I echo:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY 'fbUserReceipeId' DESC LIMIT 0,1' at line 1')' at line 1 – aiternal Dec 13 '10 at 10:57
  • @Ahmet try backticks in the table name: `\`` instead of `'` – Pekka Dec 13 '10 at 10:59
  • Oh Pekka thank you very much. It worked. But what is the difference? And what is it's shortcut :) – aiternal Dec 13 '10 at 11:02
  • @Ahmet backticks are used to quote table names – Pekka Dec 13 '10 at 11:03
  • Weird thing is I used ' instead of backtick many times with no problem. Thank you. What is shortcut for backtick – aiternal Dec 13 '10 at 11:04
  • @Ahmet I don't know, but are you sure it's not on your keyboard somewhere? It should be on the turkish layout as well, see e.g. http://frontype.com/keyboarding/540px-Computer-keyboard-Turkey.svg.png – Pekka Dec 13 '10 at 11:06
  • Oh Pekka, you are life saver. I am sure %99,9 people never needed that key. It seems like a back comma on keyboard. Thank you again. – aiternal Dec 13 '10 at 11:10
0

Check to see that the query actually succeeds before proceeding to fetch results.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Yeah it succeeds. It returns one row and one column as I wish. Everythinh seems OK, right? – aiternal Dec 13 '10 at 10:53
  • How do you determine that it succeeds? Your function will return something in every case, the return will be 0 if there is no result or an error within the query. – Dr.Molle Dec 13 '10 at 11:01
  • I tested it writing table's and field's name directly. It was work everytime with ' but this time didn't. Pekka's solution which is using backticks worked. Thanks @Ignacio and @Dr.Molle for suggestions. – aiternal Dec 13 '10 at 11:07
0

There may be an error in your SQL statement, or maybe you don't have an open database connection?

Spiny Norman
  • 8,277
  • 1
  • 30
  • 55