Call to a member function on a non object?

What is the above error?
Before anyone talk about OOP, yes I do know Object Oriented Programming, but nevertheless, the error is not very descriptive.

I mean, call to a member function on a non-object means, I guess.... Calling a function that does not belong to an object, but still, I don't get it.

This is the guilty line of code:

//get the Category
$catQuery = catQuery($SubCat);
$catQuery->doquery2($db);
list($SubCat) = $SubCat->getrow();

My apache php log errors say it's the last line, the list().

This is my function

//Returns the query which gets the listing by SubCategory
function catQuery($SubCat) {

$strSQL =
"SELECT
SubCat.Nom AS prodSubCat
FROM
Produit
WHERE
SubCat='$SubCat'";

$q = new Query();
$q->setSQL($strSQL);
return $q;

}

Can anyone help me understand what's wrong?

Thanks

    It appears that $SubCat is not an object. Are you sure you initialised it properly? Or did you just mean to type:

    list($SubCat) = $catQuery->getrow();

    for the line that's giving you the error? (Looking at your code, it seems that $SubCat is likely a string, not an object.)

      No, $subcat is supplied as a string.

      $ageId = $HTTP_GET_VARS["cboAge"];
      $priceId = $HTTP_GET_VARS["cboPrix"];
      $pieceId = $HTTP_GET_VARS["cboPiece"];
      $awardWinner = $HTTP_GET_VARS["AW"]; //A True or False value
      $newProduct = $HTTP_GET_VARS["New"]; //A True or False value
      $SubCat = $HTTP_GET_VARS["SubCat"]; //Here SUBCAT IS

      But all the others aren't objects either, they are also strings.
      I'll supply more could below, and perhaps you guys can tell me where my problem is in my code:

      /This function returns an array of products which matched the search criterion.(none,one,many)
      $products = searchProducts($db,$ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$pageIndex,$numToDisplay);

      //This function returns the total number of products matching the search criterion.
      $numFound = countProducts($db,$ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat);

      //This function returns a string of links which are used to navigate through the search results.
      $navigation = searchNavigation($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$numFound,$numToDisplay,$pagesToDisplay,$pageIndex);

      //Display, using the given templates, the given products, the navigation links and search count.
      displayPage($prodTableTemp,$prodRowTemp,$prodTemp,$products,$numFound,$navigation,$pageIndex,$typeList,$imgDir,$txtKey );

      This is all a PHP page that searches through the catalog, however I wanted to add Search by category functionality, and that is where $SubCat comes in. Am I adding it wrong? I followed the same convention as the other variables.

      //This function counts the total number of products matchin the search criteria
      function countProducts(&$db,$ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat) {

      $countQuery = countQuery($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat);
      $countQuery->doQuery2($db);
      $row = $countQuery->getrow();
          return $row["count"];

      }

      //Returns a string of links to navigate the search results.
      function searchNavigation($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$numFound,$numToDisplay,$pagesToDisplay,$curPage){

      $numPages = modCount($numFound,$numToDisplay); // determine, from the number of prods and prods/page, the number of pages of search results.
      $numSets = modCount($numPages,$pagesToDisplay);//determine, from number of pages and pages/set, the number of sets of pages of search results.
      $curSet = (int)($curPage/$pagesToDisplay); // the current set of pages being displayed.
      
      if( $curPage % $pagesToDisplay == 0 ) { //A new set starts at 1, not 0.   
      	$curSet = $curSet -1;
      }
      
      $nextSet = ""; //Image Link to first page of next set.
      $prevSet = ""; //Image Link to first page of previous set.
      $next = ""; //Image Link to next page of results.
      $prev = ""; //Image Link to previous page of results.
      $pages = ""; //String of text links to particular pages of results in the current set.
      
      
      if( $curSet > 0 ) { //A previous set of pages exist
      	$page = ($pagesToDisplay * ($curSet-1)) + 1; //first page of previous set.
      	$image = "flecheg2.gif"; 
      	$prevSet = navImageLink($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$image,$page);
      }
      
      if( $curSet < ($numSets-1) ) { //A next set of pages exist 
      	$page = (($curSet + 1 ) * $pagesToDisplay) + 1; //first page of next set.
      	$image = "fleched2.gif";
      	$nextSet = navImageLink($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$image,$page);
      }
      
      if( $curPage > 1 ) { //A previous page exists
      	$page = $curPage - 1;
      	$image = "flecheg1.gif";
              $prev = navImageLink($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$image,$page);
      }
      if( $curPage < $numPages ) { //A next page exists
      	$page = $curPage + 1;
      	$image = "fleched1.gif";
      	$next = navImageLink($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$image,$page);
      }	
      
      if ($numPages >= ($pagesToDisplay * ($curSet + 1)) ) { //Current set is at least full
      	$pagesInSet = $pagesToDisplay; //Display a full set of pages
      } else { //Current set is not full
      	$pagesInSet =  $numPages - ($curSet * $pagesToDisplay); // display remainder pages
      }
      
      $offset = $pagesToDisplay * $curSet;

      // echo "curPage = $curPage pagesInSet = $pagesInSet numPages = $numPages, numSets = $numSets curSet = $curSet setOffset = $setOffset";
      for( $i=1; $i<=$pagesInSet; $i++ ) { //loop throught the ages in the set.
      $pages .= navLink($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$curPage,($i+$offset));
      }

      //Return the navigation links & images.
          return $prevSet . $prev . $pages . $next . $nextSet;		

      }

      function writeProduct(&$prod,&$template) {

      //Get the individual data items of the product
      list($prodId,$prodName,$prodAge,$prodPieces,$prodPrice,$prodSubCat,$prodSKU,$prodThmImg,$prodPopUpImg,
      $awardProd,$newProd,$ageId,$skillId,$piecesId,$priceId) = $prod;
      
      //Fill the product HTML template with the product data.
      $template->set_var( array (
      	"PROD_ID"=>$prodId,
      	"PROD_NAME"=>$prodName,
      	"PROD_AGE"=>$prodAge,
      	"PROD_PIECES"=>$prodPieces,
      	"PROD_PRICE"=>$prodPrice,
      	"PROD_SKU"=>$prodSKU,
      	"PROD_IMAGE"=>$prodThmImg,
      	"PROD_AWARD"=>$awardProd,
      	"PROD_NEW"=>$newProd,
      	"PROD_SubCat"=>$SubCat,
      	"PROD_AGE_ID"=>$ageId,
      	"PROD_SKILL_ID"=>$skillId,
      	"PROD_PIECES_ID"=>$piecesId,
      	"PROD_PRICE_ID"=>$priceId ) );

      //This function searches the product table for products matching the given criteria.
      function searchProducts(&$db,$ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$pageIndex,$numToDisplay) {

      //Query the database for matching products
      $prodQuery = productQuery($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$pageIndex);
      $prodQuery->doquery2($db);
      $i=0;
      $products = array();
          while( $p = $prodQuery->getrow() ) {//Loop through the rows in the product table which matched the criteria.
      	$prod = createProduct($db, $p ); //Creates a product from the given data.
      	$products[$i++] = $prod; //put product into array.
      
      }
      
      return $products; //return the products found.

      }

      function createProduct(&$db,$prodRow) {

      //data elements in the $prodRow
      list($prodId,$newProd,$SubCat,$prodSubCat,$awardProd,$prodName,$prodThmImg,$prodPopUpImg,$prodSKU,$piecesId,$priceId,$ageId) = $prodRow;
      
      //get the pieces
      $pieceQuery = piecesQuery($piecesId);
      $pieceQuery->doquery2($db);
      list($prodPieces) = $pieceQuery->getrow();
      
      //get the price
      $priceQuery = priceQuery($priceId);
      $priceQuery->doquery2($db);
      list($prodPrice) = $priceQuery->getrow();
      
      //get the Category
      $catQuery = catQuery($SubCat);
      $catQuery->doquery2($db);
      list($prodSubCat) = $SubCat->getrow();
      
      //get the age
      $ageQuery = ageQuery($ageId);
      $ageQuery->doquery2($db);
      list($prodAge) = $ageQuery->getrow();
      
      //return the Product
      return array(	$prodId,$prodName,$prodAge,$prodPieces,$prodPrice,$prodSKU,$prodThmImg,$prodPopUpImg,
      				$awardProd,$newProd,$SubCat,$ageId,$skillId,$piecesId,$priceId);

      }

      //Returns the query which searches the product table
      function productQuery($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat,$pageIndex=1,$numToDisplay=6) {

      $offset = ($pageIndex-1)*$numToDisplay;
      $limit = " LIMIT $offset,$numToDisplay "; 
      $whereClause = constructWhereClause($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat);
      $strSQL =
      "SELECT DISTINCT 
      
      	prod.Produit_ID AS prodId,
      	prod.Is_New AS newProduct,
      	prod.Is_Award_Winner AS awardProd,
      	prod.Nom AS prodName,
      	prod.img_thumb_1 AS prodThmImg,
      	prod.img_pop_1 AS prodPopUpImg,
      	prod.SKU AS prodSKU,
      	prod.Number_Piece_Range_ID AS piecesId,
      	prod.Price_Range_id AS priceId,
      	prod.List_Group_Age_ID AS ageId,
      	prod.SubCat as SubCat
      
      FROM
      	Produit prod,
      	Groupe_Age age
      
      
      WHERE
      
      ".$whereClause." 
      ORDER BY 
      	prod.Nom ASC
      
      ". $limit;
      $q = new Query();
      $q->setSQL($strSQL);
      return $q;

      }

      function countQuery($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat) {

      $strSQL = "
      
      SELECT COUNT(DISTINCT Produit_ID) AS count
      
      FROM 
      	Produit prod,
      	Groupe_Age age
      
      WHERE " .constructWhereClause($ageId,$priceId,$pieceId,$awardWinner,$newProduct,$SubCat);
      
      $q = new Query();
      $q->setSQL($strSQL);
      return $q;

      }

        Below is the rest of the code:

        //Constructs the where clause for the product search query
        function constructWhereClause($ageId="",$priceId="",$pieceId="",$awardWinner="",$newProduct="",$SubCat="") {

        $i = 0; //Count the number of clauses
        $whereStmts = array();//an array to hold each clause
        
        if( $ageId != "" ) {
        	$whereStmts[$i++] = " prod.List_Group_Age_Id = age.Groupe_Age_ID ";
        	$whereStmts[$i++] = " age.ageCatId = '$ageId' ";
        }
        
        if( $priceId != "" ) {
        	$whereStmts[$i++] = " prod.Price_Range_id = '$priceId' ";
        }
        
        if( $pieceId != "" ) {
        	$whereStmts[$i++] = " prod.Number_Piece_Range_ID = '$pieceId' ";
        }
        
        if( $awardWinner != "" ) {
        	$whereStmts[$i++] = " prod.Is_Award_Winner = '$awardWinner' ";
        }
        
        if( $newProduct != "" ) {
        	$whereStmts[$i++] = " prod.Is_New = '$newProduct' ";
        }
        
        if( $SubCat != "" ) {
        	$whereStmts[$i++] = " prod.SubCat = '$SubCat' ";
        }
        
        $whereStmts[$i++] = " prod.Is_Active = '1' ";
        
        
        $numClauses = $i;
        $whereClause = "";
        for( $j = 0; $j <$numClauses - 1; $j ++ ) { // loop through each clause, construct proper SQL
        	$whereClause .= $whereStmts[$j]. " AND ";
        }
        
        $whereClause .= $whereStmts[$numClauses-1];
        return $whereClause;

        }

        //Returns the query which gets the price name from the prices table
        function priceQuery($priceId) {

        $strSQL =
        "SELECT
        
        	price.Nom AS prodPrice
        FROM
        	Price_Range price
        WHERE
        	price.Price_Range_ID='$priceId'";
            $q = new Query();
            $q->setSQL($strSQL);
            return $q;

        }

        //Returns the query which gets the listing by SubCategory
        function catQuery($SubCat) {

        $strSQL =
        "SELECT
        	SubCat.Nom AS prodSubCat	
        FROM
        	Produit SubCat
        WHERE
        	SubCat='$SubCat'";
        
        
            $q = new Query();
            $q->setSQL($strSQL);
            return $q;

        }

        //Returns the query which gets the age name from the ages table
        function ageQuery($ageId) {

        $strSQL =
        "SELECT
        	age.Nom AS prodAge
        FROM
        	Groupe_Age age
        WHERE
        	age.Groupe_Age_Id='$ageId'";
        
            $q = new Query();
            $q->setSQL($strSQL);
            return $q;

        }

        ?>

        Looking at this code, can anyone tell me where am I going wrong?
        If $SubCat is a string and not an object and that is causing trouble, how come it doesn't call it for the other strings? like $ageid?

        Thanks
        MK
        Plz Help me dudes.

          if $subcat is supplied as a string, then you can't whrite $SubCat->getrow() as this means you're invoking the member function getrow() of the $SubCat object.

            Oh yes, i'm sorry, I meant

            list($prodSubCat) =$catQuery->getrow();

            $catQuery is an object.
            I don't get the non an object error anymore, but the problem is my resultset is wrong.

            After I added $SubCat, the images being retrieved are wrong and I still don't get a query which adheres to $SubCat.

            So if subcat=3, I don't get listings where subcat==3 in the database.

            In my Apache PHP Error logs, I get an Undefined Offset: 11
            Below is the incriminating line:

            //data elements in the $prodRow
            list($prodId,$newProd,$SubCat,$prodSubCat,$awardProd,$prodName,$prodThmImg,$prodPopUpImg,$prodSKU,$piecesId,$priceId,$ageId) = $prodRow;

            The error gives the number 11, which was the previous number of elements in list(). Now it's 12, probably when I added $SubCat, perhaps I need to add somewhere else where I didn't.
            But I can't see where.

            Is that is what is causing the problem?

            Any idea why looking at my code?

            Thanks

              Write a Reply...