php - Returning an array with PDO - using FetchAll doesn't work -
i use following code retrieve data database. problem displays first row. in particular case, means first picture shown on webpage want show of them.
<?php $sql = "select `image-id`, `article-id`, `image-path`, `image-title` `table-images` `article-id` = :id"; $stmt = $pdo->prepare($sql); $stmt->bindparam(":id", $id); $stmt->execute(); if($result = $stmt->fetch(pdo::fetch_assoc)) { ?> <a class="swipebox" href="<?php echo $result['image-path'];?>" title="<?php echo $result['image-title'];?>"> <img alt="image" src="<?php echo $result['image-path'];?>"></a> <?php }// end if else { echo '0 results'; }// end else ?>
i read this article tried use code:
if($result = $stmt->fetchall(pdo::fetch_assoc));?
... doesn't work. doesn't echo first picture anymore. missing here?
here how works:
$stmt = $pdo->prepare($sql); $stmt->bindparam(":id", $id); $success = $stmt->execute(); if($success){ //fetch here }
now have 2 options fetching data:
fetch()
fetch()
rows 1 one need while loop.
while($rows = $stmt->fetch(pdo::fetch_assoc)){ // data }
fetchall()
fetchall()
rows @ once no need loops retrieve data, if need loop need loop.
$rows = $stmt->fetchall(pdo::fetch_assoc); foreach($rows $row){ //do }
Comments
Post a Comment