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

1
  • Seems like your SQL query is failing. Commented Dec 13, 2010 at 10:55

3 Answers 3

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.

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

7 Comments

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
@Ahmet try backticks in the table name: ` instead of '
Oh Pekka thank you very much. It worked. But what is the difference? And what is it's shortcut :)
@Ahmet backticks are used to quote table names
Weird thing is I used ' instead of backtick many times with no problem. Thank you. What is shortcut for backtick
|
0

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

3 Comments

Yeah it succeeds. It returns one row and one column as I wish. Everythinh seems OK, right?
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.
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.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.