0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

I tried everything I could think of, but I keep on getting this error.

Mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /url/ on line 41

if ( $_POST[submit] == "Submit" )
    {
        $sql="INSERT INTO table (`content`, `userid`, `ttime`) VALUES 
('$_POST[content]', '".$user_id."', '".time()."')";
    $res = mysql_query($sql,$link) or die(mysql_error());
/* (line 41 is the following)*/ 
while($result = mysql_fetch_assoc($res)) {
    } }

I tried printing out the error (no error prints out just the warning), I tried changing the query, everything I could think of. The code works just fine - it does the insert on click, everything is fine, just that warning is appearing ._.'

Any ideas?

Community
  • 1
  • 1
Izumi
  • 571
  • 1
  • 7
  • 16

3 Answers3

1

You can't fetch a result from an INSERT query.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

You are attempting to obtain a data row from a query that is not a SELECT query. You can only fetch associated arrays from a result data set. An INSERT query just does its thing.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
1

From php documentation on mysql_query()

Return Values

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.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90