Jump to content

MYSQLI, binding results problem


russianbear

Recommended Posts

Hoping somebody can help me out.

 

What I'm trying to do is query my db and pull from it a title and ID of a posting. 

The web app I'm building is a mini version of the craigslist.com

 

Basically the user enters a bunch of information about a product they are trying to sell including pictures, etc...

The LOC you see below will query the db based on user input and return a result set of ALREADY posted listings...so basically I'm trying to print out the titles of the postings and make those hyperlinks to the actual posts.

 

The problem I'm running into isn't in the MySQL query - I already tested that and it works, but in my PHP logic in capturing the data after the query is executed!

Okay - here is the rub.  This code works, but only for a specific set of input.  The input is captured on the prior page and it comes from 2 pull down menus that provides the SubCategoryID (i.e. option 0 = books, option 1 = art, option 2 = etc...) and the LocationID (option 0 = Cupertino, CA ; option 1 = Moscow, Russia, etc...)  The results are ONLY displayed if the user chooses: LocationID = 0 and SubCategory = 0 (another words the code below only works if the user selected Cupertino and Books on the prior page).

 

Is the problem obvious or do you need more information to further diagnose my stupidity? :shrug:

 

Thanks in advance!  PS - I'll send a darn good bottle of Pinot to whoever can help me solve this issue!  And if you don't drink, I'll send ya a couple of 2 liters of Mountain Dew.  :o  And if you don't drink caffeine either, well then you'll just have to settle with my thanks and BIG UPS!!!! :P

 

if($stmt = $conn->prepare("SELECT Post_ID, Title FROM POSTS as p, 
                                     LOCATION as l, SUBCATEGORY as s, REGION as r, CATEGORY as c 
                                     WHERE((p.Location_ID=?) AND (p.SubCategory_ID=?) AND (r.Region_ID=?) 
                                     AND (p.SubCategory_ID = s.SubCategory_ID)
                                     AND (p.Location_ID = l.Location_ID)
                                     AND (s.Category_ID = c.Category_ID) 
                                     AND (l.Region_ID = r.Region_ID))")) {   
      
      // Bind your variable to replace the ?
      $stmt->bind_param('iii', $field1, $field2, $field3);

      // Set your variables        
      $field1 = $Sub_Category_ID;       
      $field2 = $Location_ID;
      $field3 = $Region_ID;

      $row = array();
      //Bind the result
      $stmt->bind_result($row['post_id'],
                         $row['title']);

      //Execute the query
      $stmt->execute();
                  
      //Okay, got the result, how output them to the screen
      // Output here looks like USA->Cupertino->For Sale->Automobiles
      echo "<h3>$Region_Name -> $inputArray[location] -> $Category_Name -> $inputArray[sub_category]</h3>";

       //here is where I'm getting the ID and title and storing them in a local 2-D array      
      while($stmt->fetch()) {
         foreach($row as $key => $value) {
            $tmp[key] = $value;
         }//end foreach
         $this->PostArray[$count++] = $tmp;
      }//end while

    // PROBLEM IS - WHEN I attempt to print the array recursively, I get NOTHING!!!!!
    // Can you see anything in my logic that is BOGUS, for lack of better words.  
      print_r($PostArray);
   }//end if
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.