I'm trying to get a parent category for a given sub category using the following function in PHP.
require_once("Connection.php");
$flag=true;
function get_parent_id($cat_id, $parent_id)
{
if ($parent_id==0)
{
return($cat_id);
}
else if ($flag==true)
{
$data1=mysql_query("select parent_id from category where cat_id=" + $cat_id);
while($row = mysql_fetch_assoc($data1))
{
$parent_id=$row['parent_id'];
}
$flag = false;
}
else if ($flag==false)
{
$data2=mysql_query("select cat_id from category where cat_id=" + $parent_id);
while($row = mysql_fetch_assoc($data2)) //The warning comes from here.
{
$cat_id=$row['cat_id'];
}
$flag = true;
}
$cat_id = get_parent_id($cat_id, $parent_id);
return($cat_id);
}
}
echo get_parent_id($ed_id, $parent_id); //Call the above function.
It always prompts the following warning.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource in C:\wamp\www\zoom\Category.php on line 492
Even though there is no error in SQL. The included file Connection.php also works fine on all other pages. I didn't understand at all why this happens.