tags:

views:

85

answers:

3

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?

+1  A: 

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

Ignacio Vazquez-Abrams
Then how am I supposed to do it?
Izumi
What exactly is "it" supposed to be?
Ignacio Vazquez-Abrams
>.<' Just re-read the mysql_fetch_assoc info and how it's used. Sorry and thanks >.< I thought it was supposed to be used with insert and select ><' *newb*
Izumi
+1  A: 

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
Thank you, thought I should use it for both ><!
Izumi
+1  A: 

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