简体   繁体   中英

Reusing php prepared statement will only return last entry of mysql table

I'm encountering a strange problem. To be honest this is the first time I try to re-use a prepared statement on the same php page. In this case I really need it to work since the solution of doing 2 identical statements with different variable names seems redundant. Anyway Here's what I did.

    $mysqli = new MySQLi(HOST,USER,PASSWORD,DATABASE);
    $image_from_database = $mysqli->prepare("SELECT slider_id, slider.images_id, images.images_id, images.image_name,
                                                    images.image_path, images.image_type, images.image_size,
                                                    image_tag_name, image_tags.date FROM slider
                                             LEFT JOIN images ON slider.images_id = images.images_id
                                             LEFT JOIN image_tags ON image_tags.images_id = images.images_id");
    $image_from_database->bind_result($sliderID,$sliderImageID,$imageID,$imageName,$imagePath,$imageType,$imageSize,$tagName,$tagDate);
    $image_from_database->execute();
$image-from_database->store_result();

Then for the call:

<div class="large-6 columns">
<fieldset class="slider_control">
<legend>Images From Database</legend>
<div class="slider_image_edit">
<div class="slider">
<?php 
while($image_from_database->fetch()){ 
if(!empty($sliderID)){ 
echo'
<div><img src="../'.$imagePath.'"></div>';
    }
}
$image_from_database->free_result();
?>
    </div>

Then later on the page I want to display the results in a table instead of an image slider...

<fieldset>
<legend>Choose the different Images to edit, you can view the slider to see the order they are in</legend>
<form action="" method="post" enctype="multipart/form-data">
<table>
<thead>
<th>ID</th>
<th>Name</th>
<th>Tag</th>
<th>Type</th>
<th>Size</th>
<th>Edit</th>
<th>Delete</th>
</thead>
<tbody>
<?php
$image_from_database->execute();
while($image_from_database->fetch()){ 
echo '
<tr>
<td>"'.$sliderID.'"</td>
<td>"'.$imageName.'" </td>
<td>"'.$tagName.'" </td>
<td>"'.$imageType.'" </td>
<td>"'.$imageSize.'" </td>
<td>Edit</td>
<td>Delete</td></tr>';
}
?>
</tbody>
</table>
</form>
</fieldset>

When I load the page, the slider will display perfectly with all the corresponding images, but on the list it will only display the last row of the table in this particular example it will display something like:

ID  Name    Tag Type    Size    Edit    Delete
"2" "bf535be38b469cfbcf6db6d02ba3a9d0.jpg"  "Colleges & Programs"   "image/jpeg"    "82993" Edit    Delete
"2" "bf535be38b469cfbcf6db6d02ba3a9d0.jpg"  "Colleges & Programs"   "image/jpeg"    "82993" Edit    Delete

The edit and delete dont do anything at this time, they are just there as placeholders for what I want later on.

Anyway, can someone tell me why the query will work on the first part of the page but not on the second one?

This is because you already showed all records and the array you got from the database is already at it's end. So when you call it again, it will start with the last.

Do what Jeroen told you, store the result in an variable or something and then reset it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM