4 Replies - 2816 Views - Last Post: 21 July 2011 - 05:26 PM Rate Topic: -----

#1 biggles2008  Icon User is offline

  • Bassface
  • member icon

Reputation: 9
  • View blog
  • Posts: 623
  • Joined: 05-March 08

mysql_fetch_array() expects parameter 1 to be resource

Posted 21 July 2011 - 03:19 PM

Hi, I'm attempting to create a personal blog just to get myself back into web development again. I'm a bit of a novice I must admit. I've had this problem before and solved it however I cannot seem to remember why the error is occurring.

Here is my code:
<?php $product = mysql_query("SELECT * FROM 'article' LIMIT 0, 30");
			
			while($row = mysql_fetch_array($product))
			{
				$post_title = $row['title'];
				$post_content = $row['comments'];
				$post_date = $row['date_posted'];
			echo <<<_END
            <div class="post">
			<div class="post-content">
			<h1>$post_title</h1>
			<div class="post-comment">$post_content</div>
			<div class="post-timestamp">$post_date</div>
			<div class="post-category">#web design</div>
            </div>
            <div class="clear"></div>
			</div>
_END;
		}



I thought it best I screenshot the error:
Attached Image

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: mysql_fetch_array() expects parameter 1 to be resource

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: mysql_fetch_array() expects parameter 1 to be resource

Posted 21 July 2011 - 03:24 PM

your query failed. ' is not for quoting column names, use instead.
Was This Post Helpful? 1
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: mysql_fetch_array() expects parameter 1 to be resource

Posted 21 July 2011 - 03:25 PM

mysql_query return false upon failure so you can check like so:
if($product = mysql_query("SELECT * FROM 'article' LIMIT 0, 30"))
{

}
else
{
  echo mysql_error();
}



Even better would be to put your SQL in a variable so you can echo it out upon failure and run it through MySQL itself to see what sort of message pops up
 $sql = "SELECT * FROM 'article' LIMIT 0, 30";
 if($product = mysql_query($sql))
 {

 }
 else
 {
  echo "problem with query $sql";
 }



Also, you may want to look into moving away from the mysql* functions for database access and use something like PDO instead.
Was This Post Helpful? 1
  • +
  • -

#4 biggles2008  Icon User is offline

  • Bassface
  • member icon

Reputation: 9
  • View blog
  • Posts: 623
  • Joined: 05-March 08

Re: mysql_fetch_array() expects parameter 1 to be resource

Posted 21 July 2011 - 05:17 PM

Thank you for your quick and helpful responses. My problem was solved using both of your advice.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: mysql_fetch_array() expects parameter 1 to be resource

Posted 21 July 2011 - 05:26 PM

623 posts, an Author, and yet you you don't know to read the FAQS???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1