Jump to content

returning no results from a SELECT WHERE ORDER by LIMIT mysql query


james909
Go to solution Solved by Barand,

Recommended Posts

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)

Edited by james909
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.