Mysqli loop within another loop, not working

I have a page with a working loop showing some data perfectly fine…it is a customer list. But…I have a separate database table with the customer phone numbers in it and for some reason the phone numbers wont show up.

I do have to say, I have done this with success using mysqli_query, and then attempted to change my code to mysqli_prepare method (learned this was better way to do it) and it now does not work.

The start of my first loop…which works just fine.
[php]$query = “SELECT id, firstname, lastname, datecreated, email”;
$query .= " FROM customers";
$query .= " ORDER BY datecreated DESC";
$query .= " LIMIT 20";
$query .= " OFFSET ?";
$stmt = mysqli_prepare($db, $query);
mysqli_stmt_bind_param($stmt, ‘i’, $offset);

$query2 = “SELECT phone, type, notes”;
$query2 .= " FROM customerphones";
$query2 .= " WHERE ‘customer_id’ = ?";
$query2 .= " LIMIT 4";
$stmt2 = mysqli_prepare($db, $query2);

mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $custid, $fname, $lname, $datecreated, $email);

while(mysqli_stmt_fetch($stmt)) {

// this works fine…2nd loop is inside here…

}
[/php]

And then my second loop within this one…not working :frowning:

[php]mysqli_stmt_bind_param($stmt2, ‘i’, $custid);
mysqli_stmt_execute($stmt2);
mysqli_stmt_bind_result($stmt2, $phone, $type, $notes);
while(mysqli_stmt_fetch($stmt2)) {

//…not working :frowning:

}[/php]

I did put my prepared statements outside of the first loop…was told you shouldn’t put them within loops. But when I tried putting it just above the second loop (and therefore within the first loop), a couple errors popped up (saying mysqli_stmt_bind_param needed a stmt and not a boolean…) So I’m thinking my $stmt2 is just failing somehow. But I am lost.

Can anyone help me out? :o

I can post the entire page if it would help clarify more…hoping just this bit of code will reveal my issue though

It’s been a couple days and it seems no one can help me.

I would just like to note that I changed my $query2 to this:

[php]
$query2 = “SELECT phone, type, notes”;
$query2 .= " FROM customerphones";
$query2 .= " WHERE customer_id=?";
$query2 .= " LIMIT 4";
$stmt2 = mysqli_prepare($db, $query2);[/php]

And it works separately if I test it on another page where it is not within the customers loop.

It looks like I will have to change my prepared statements back into mysqli_query :frowning:

I think the reason no one is helping you is they don’t know what you are really trying to do/accomplish. If you be a little more descriptive than loop 2 isn’t working, it might help get the answer/solution that you are looking for.

I’m just curious, why do you do this
[php] $query2 = “SELECT phone, type, notes”;
$query2 .= " FROM customerphones";
$query2 .= " WHERE customer_id=?";
$query2 .= " LIMIT 4";[/php]

[php]$query2 = “SELECT phone, type, notes FROM customerphones WHERE customer_id=? LIMIT 4”;[/php]
To me the second way makes it easier to change and decipher, I’m just curious?

Thank you for a response!

That is just the way I learned it…lol It makes sense to me, so it’s just what I do. In my opinion, the first way is easier to read.

And I don’t know how to be much more descriptive, it isn’t showing any error codes, just not displaying the data…and I’m not really sure how to debug it at this point (I’m still kinda new to PHP) because I’ve just learned about prepared statements and am not too familiar with them…I can only just barely get them to work.

So…after a little bit of research, I think I’ve concluded that you just simply CANNOT loop a query from a prepared statement within another one (correct me if I’m mistaken). So switching it back so I’m using mysqli_query to fetch my data accomplishes my task, I will just need to implement extra security measures I guess.

If your selecting data based on the customer_id why would you limit the result to 4? Assuming you have more than 4 records, you are always only going to get the last 4 out of X records.

Yup, this is on purpose. I only want it to show 4. It is just a pre-view of a customer, and then there is a link to view the full profile, which lists them all.

I would like to add that I have solved my own problem! :slight_smile:

For one, I am doing it the OOP way now, because it reads much easier.

I was missing, before my loop, the:

[php]$stmt->store_result();[/php]

It seems that’s all I was doing wrong. I can now use the data properly.

Thought I’d add my solution in case anyone else encounters this…it gave me a lot of trouble.

Sponsor our Newsletter | Privacy Policy | Terms of Service