Jump to content

Recommended Posts

Ok this one's really odd. Even though i can run the query in phpmyadmin without errors when i try to call it using mysql_fetch_array it gives the error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/hogen/public_html/development/v1/index.php on line 78

Also yes, mysql is working on other queries on the page, just this one doesn't want to work. I've tried the or die(mysql_error()) with no error revealed.

 

Here's the code:

	//Global Functions
	require("./includes/init.php");

	//Lets Loop!
	$news = mysql_query("
	SELECT id, category, title, article, userid, dateposted
	FROM newsposts
	WHERE category = 1
	ORDER BY id
	");

	while ($news = mysql_fetch_array($news, MYSQL_ASSOC))
	{
			$tpl->assign("loop", array(array("title" => "{$news['title']}", "content" => "{$news['article']}", "userid" => "{$news['userid']}", "userid" => "{$news['date']}")));
	}

 

Help, please :)

Link to comment
https://forums.phpfreaks.com/topic/168915-solved-mysql_fetch_array-giving-error/
Share on other sites

Your code is terrible. Always check your results before trying to use them. eg;

 

if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    // it is now safe to call mysql_fetch_*
  } else {
    // no results where found
  }
} else {
  // your query failed
}

 

Try this:

if ($result = mysql_query($sql))
{
    if (mysql_num_rows($result))
    {
        echo 'Everything works.';
        echo '<br />';
        echo $sql;
    }

    else
    {
        echo 'No results were found.';
        echo '<br />';
        echo mysql_error() . " : " . $sql;
    }
}

else
{
    echo 'MySQL query failed.';
    echo '<br />';
    echo mysql_error() . " : " . $sql;
}

So would this be more logical?

 


if ($result = mysql_query($sql))
{
    if (mysql_num_rows($result))
    {
        echo 'Everything works.';
        echo '<br />';
        echo $sql;
    }

    else
    {
        echo 'No results were found.';
        echo '<br />';
        echo $sql;
    }
}

else
{
    echo 'MySQL query failed.';
    echo '<br />';
    echo mysql_error() . " : " . $sql;
}

Yeah, except I still see no reason to echo the $sql variable. You really shouldn't be echoing a mysql_error() either within a production environment. Your better off triggering an error (passing it the output of mysql_error()) and then catching that later. this way you can handle error gracefully (and log them) within production, or display them easily during development.

Hmmmm

MySQL query failed.
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 'Resource id #10' at line 1 :

From what i found on that error it seems to be because the query is called by mysql_query and not fetch.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.