Jump to content

Easy query?


bofett

Recommended Posts

Please help.

Table (cats)  has id, name,  parent.

I am trying to echo the name of the parent when I know the id of the child.  So for example…

id  name          parent

1  Automotive  NULL

2  Cleaning          1

3  Repair           1

 

I would like to display “ Automotive – Cleaning”  When I know id=2.  What is the best way to query for “Automotive”?

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/144420-easy-query/
Share on other sites

  • 2 months later...

This did the trick...

 

	function getCat($cat){
	$qr1 = mysql_query("SELECT name FROM cats WHERE id='$cat'");
	$a = mysql_fetch_object($qr1);
	return $a->name;
}
function getsubcat($cat){
	$qr1 = mysql_query("SELECT parent FROM cats WHERE id='$cat'");
	$a = mysql_fetch_object($qr1);

	if ($a->parent != "") {
	$qr2 = mysql_query("SELECT name FROM cats WHERE id=$a->parent");
	$a2 = mysql_fetch_object($qr2);
	echo "{$a2->name} - ";
 }else{
        }
  }

Link to comment
https://forums.phpfreaks.com/topic/144420-easy-query/#findComment-825259
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.