0

I Built a 2D array which basically grabs information from a mysql database. The First While loop, loops through DB for info and unique IDs from some rows, works fine. The inner while loop then grabs info from another table when as certain condition is met, this is where I have the issue. I get issue when I use bind_param() inside the inner loop. I have minimized the code so all the irrelevant conditions and line aren't taking up the page.

Here is my error

Commands out of sync; you can't run this command now

Fatal error: Call to a member function bind_param() on a non-object

Here is the basics of my loops

function propertyFeedIndex($conn, $ListingAgentID)
{


    $sql = "SELECT ListingID FROM mls_listings_phrets WHERE ListingAgentID = ? order by id desc";

    $stmt = $conn->prepare($sql);

    $stmt->bind_param('s', $ListingAgentID);

    $stmt->execute();

    $stmt->bind_result($ListingID);

    if($stmt->fetch()){     

        while($stmt->fetch()){

            // Get Images
            $sql_img = "SELECT C_PATH FROM mls_listings_images_phrets WHERE C_Matrix_Unique_ID = ? order by `id` asc";

            $stmt_img = $conn->prepare($sql_img);
            echo $conn->error;

            $stmt_img->bind_param('s', $ListingID);

            $stmt_img->execute();

            $stmt_img->bind_result($C_PATH);

            if($stmt_img->fetch()){

                while($stmt_img->fetch()){

                    echo "Help Me " . $C_PATH;

                }
            }   

        }

    }else{
        echo "We Have Issues!";
    }

    return $finalArray;
}
  • I only see one `while` loop. – Barmar Sep 11 '15 at 19:10
  • You're skipping the first row of the first query results, because `if ($stmt->fetch())` fetches the first row, and then `while ($stmt->fetch())` starts at the second row. – Barmar Sep 11 '15 at 19:11
  • Which call to `bind_param` is getting that error? That means that the `prepare()` function is getting an error, and you need to show `$conn->error`. – Barmar Sep 11 '15 at 19:12
  • Instead of two queries, you can do one query with a `JOIN`. – Barmar Sep 11 '15 at 19:13
  • You don't need to call `prepare` every time through the loop, since it's the same query each time. Call `prepare` and `bind_param` once before the loop, and just call `execute` inside the loop. – Barmar Sep 11 '15 at 19:15

0 Answers0