james909 Posted May 4, 2013 Share Posted May 4, 2013 i am trying to getting the results from mysql database 'userdatabase' where the user_type column match and is_approved field is 1. i am trying to get the top 5 rows with the highest average_ratings. here is my code: $mysqli = new mysqli("localhost", "levelDbUser", "honey", "levelDb"); $outpostlevel = (int) $array[user_type]; $sql = "SELECT id, image_type, total_ratings, average_rating FROM userdatabase WHERE user_type = ? && is_approved = 1 ORDER by average_rating ASC LIMIT 5"; $statement = $mysqli->prepare($sql); $statement->bind_param("i", $outpostlevel); $statement->execute(); $statement->bind_result($id, $image_type, $total_ratings, $average_rating); $statement->store_result(); while ($statement->fetch()) { $topoutposts = array( 'id' => $id, 'image_type' => $image_type, 'total_ratings' => $total_ratings, 'average_rating' => $average_rating ); } print_r ($topoutposts); but the print_r array is returning nothing (blank) Link to comment https://forums.phpfreaks.com/topic/277619-returning-no-results-from-a-select-where-order-by-limit-mysql-query/ Share on other sites More sharing options...
james909 Posted May 4, 2013 Author Share Posted May 4, 2013 i fixed it, needed single quotas around is_approved = '1' Link to comment https://forums.phpfreaks.com/topic/277619-returning-no-results-from-a-select-where-order-by-limit-mysql-query/#findComment-1428166 Share on other sites More sharing options...
james909 Posted May 4, 2013 Author Share Posted May 4, 2013 it is only returning the first mysql row in the results, like this: Array ( [id] => 1 [image_type] => jpg [total_ratings] => 17 [average_rating] => 8.6 ) how do i get the results to be a multi-dimensional array like this: Array ( [1] => Array( [id] => 1 [image_type] => jpg [total_ratings] => 17 [average_rating] => 8.6 ) [2] => Array( [id] => 2 [image_type] => jpeg [total_ratings] => 10 [average_rating] => 7.2 ) [3] => Array( [id] => 3 [image_type] => png [total_ratings] => 8 [average_rating] => 9.3 ) [4] => Array( [id] => 4 [image_type] => jpg [total_ratings] => 12 [average_rating] => 5.2 ) [5] => Array( [id] => 5 [image_type] => png [total_ratings] => 15 [average_rating] => 8.1 )) so that all 5 rows from the results are displayed Link to comment https://forums.phpfreaks.com/topic/277619-returning-no-results-from-a-select-where-order-by-limit-mysql-query/#findComment-1428174 Share on other sites More sharing options...
Barand Posted May 4, 2013 Share Posted May 4, 2013 $topoutposts[] = array( 'id' => $id, 'image_type' => $image_type, 'total_ratings' => $total_ratings, 'average_rating' => $average_rating ); Link to comment https://forums.phpfreaks.com/topic/277619-returning-no-results-from-a-select-where-order-by-limit-mysql-query/#findComment-1428179 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.