Jump to content

First time trying JOIN - fetch_array() expects parameter 1 to be resource


Nysaan

Recommended Posts

Hi.

It's the first time i try JOIN in MySQL integrated with PHP.

I'm also a newbie @PHP so please bare with me...

 


mysql_select_db("dbnamehere", $con);
$result =  mysql_query("
SELECT *
FROM blogg
JOIN blogg_categories
ON (blogg.category=blogg_categories.id)
ORDER BY id DESC");

while($row = mysql_fetch_array($result))
{

 

I keep getting this error:

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in xxxxxxxxxxxxxxxxxx on line49.

Line 49 is "While"

hi,

 

Seems mysql_query is returning an error, i mean a false value to $result. Just try removing that join and give a simple query like

 

"SELECT * FROM blogg" and see if it works.

 

If it works,  try replacing

 

ON (blogg.category=blogg_categories.id)

 

with

 

ON (`blogg`.`category`=`blogg_categories`.`id`)

 

those are backticks not single quotes by the way.

 

Regards,

Sandeep.

 

It works using a simply query. I've tried that before and I did it again after reading your post.

I tried the code you gave me:

 

ON (`blogg`.`category`=`blogg_categories`.`id`)

 

but that didn't work either.

 

But after looking in my database I relized my misstake pretty quick.

Since I've got ID in both tables (blogg and blogg.categories) it didn't know wich one to order by.

 

So I changed:

 

ORDER BY id DESC

 

to

 

ORDER BY blogg.id DESC

 

And now it works :)

Sorry about that,

 

I should have told you to use mysql_error() to get the last mysql error like

 

mysql_query("

SELECT *

FROM blogg

JOIN blogg_categories

ON (blogg.category=blogg_categories.id)

ORDER BY id DESC")  or die(  mysql_error() );

 

regards,

sandeep.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.