Page 1 of 1

trouble with mysql_fetch_array in a function

Posted: Sun Jan 24, 2010 7:09 pm
by mechanical_angel
Hello all,

I am having a bit of a struggle with a function that uses mysql_fetch_array().

Code: Select all

 
function sql_query($query)
{
    $result = mysql_query($query) or die('SQL query error '.$query.' '.mysql_error());
    $data = mysql_fetch_array($result);
    mysql_close();
    return array($result, $data);
}
 
This results in
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in functions.php on line 5

Re: trouble with mysql_fetch_array in a function

Posted: Sun Jan 24, 2010 8:10 pm
by JakeJ
It might help if you post your actual query. If there is no data or the query is malformed, you'll have nothing to fetch.

Re: trouble with mysql_fetch_array in a function

Posted: Sun Jan 24, 2010 8:14 pm
by John Cartwright
You are closing the connection (thus terminating the resources) before attempting to use them.

Re: trouble with mysql_fetch_array in a function

Posted: Sun Jan 24, 2010 9:22 pm
by mechanical_angel
Actually I got all mixed up and was trying to use this with an INSERT query when I thought I was feeding the function with a SELECT query, hence the glitch.
It's all working fine now, thanks!