trouble with mysql_fetch_array in a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mechanical_angel
Forum Newbie
Posts: 2
Joined: Sun Jan 24, 2010 7:03 pm

trouble with mysql_fetch_array in a function

Post 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
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: trouble with mysql_fetch_array in a function

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: trouble with mysql_fetch_array in a function

Post by John Cartwright »

You are closing the connection (thus terminating the resources) before attempting to use them.
mechanical_angel
Forum Newbie
Posts: 2
Joined: Sun Jan 24, 2010 7:03 pm

Re: trouble with mysql_fetch_array in a function

Post 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!
Post Reply