php - PDO, MySQL - How do I return an array from a function? -
i using function retrieve multiple rows mysql database. use foreach loop through each match. want able add id of each row array , return array calling program use id's.
this far have tried get, going in correct direction?
$resultarray = array(); $resultarray[] = get_post_data($post_id); print_r($resultarray); code in function:
$stmt = $dbh->prepare("select * mjbox_images join mjbox_posts using (post_id) post_id = ?"); $stmt->bindparam(1,$post_id); $stmt->execute(); $resultarray = array(); foreach($stmt $row): $resultarray[] = $img_id = $row['img_id']; endforeach; return $resultarray; thanks
you need use:
while($row = $stmt->fetch(\pdo::fetch_assoc)) { } instead of loop. highly encouraged use { , } things loops :)
Comments
Post a Comment