0

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.

1
  • Try replacing the $data2 query with "select cat_id from category where cat_id=$parent_id" If to no avail, echo/print the value of $data2 and make sure that it's formatted properly as a SELECT request. Commented Sep 30, 2012 at 0:52

1 Answer 1

7

Strings are concatenated with ., not +.

String Operators

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.