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
[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
}[/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