Jump to content

PHP Joins?


l3rodey

Recommended Posts

Hi guys, I never learnt joins, and this is what my code looks like as a result, Can someone just show me how to convert the following from 2 queries to one? It is a blog post and the second query is getting an author I have shortened it for convenience. 

 

Thanks in advance.

$getBlogs = mysql_query("SELECT * FROM blogs WHERE blogStatus='1'", $retreat);
		while($row = mysql_fetch_array($getBlogs)){
			$blogID = $row['blogID'];
			$authurID = $row['authurID'];
		}

		$getAuthor = mysql_query("SELECT * FROM blog_authurs WHERE authurID='$blogAuthur' LIMIT 1", $retreat);
			while($row = mysql_fetch_array($getAuthor)){
				$authurName = $row['authurName'];
			}
Link to comment
https://forums.phpfreaks.com/topic/290812-php-joins/
Share on other sites

So you are getting the authurID from the first query and using it in the second query?  OR, are you using the variable $blogAuthur to populate the second query?  I will go on the assumption that it is the former.

 

"SELECT
   b.blogID, a.authurName
 FROM
   blogs AS b
 JOIN
   blog_authurs AS a
 ON
   b.authurID = a.authurID
 WHERE
   blogStatus = 1
"
Link to comment
https://forums.phpfreaks.com/topic/290812-php-joins/#findComment-1489702
Share on other sites

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.