Jump to content

bind the rows from database with mysqli->prepare


ArshSingh

Recommended Posts

so what im doing is ; im trying to get the rows from database with my following php code , and display them like ; 

<?php echo $col;?>

 , but its not working and giving me a blank result , a help would be greate thanks :

          <?php 
 $stmt = $mysqli->prepare("SELECT * FROM `movies` ORDER BY `date` DESC LIMIT 0, 4"); 
          $stmt->execute(); // Execute the prepared query.
$stmt->store_result();
if($stmt->num_rows == 1) {
$stmt->bind_result($id,$title,$poster,$date); // get variables from result.
         $stmt -> fetch();
}
?>

since your sql query statement is trying to return LIMIT 0, 4 rows, what's the chance of there being just one row and for if($stmt->num_rows == 1) { to be true?

 

also, the variable you are trying to echo, $col, isn't one you are binding, so there's no way $col will contain any value that the query selected.

since your sql query statement is trying to return LIMIT 0, 4 rows, what's the chance of there being just one row and for if($stmt->num_rows == 1) { to be true?

 

also, the variable you are trying to echo, $col, isn't one you are binding, so there's no way $col will contain any value that the query selected.

$col was for example only  , i have removed  if($stmt->num_rows == 1) { , adn now its giving me only 1 result , but i want 4 first rows from databse

we can only help you with your actual code. posting anything else in a thread just sidetracks the problem.

its always from this thread , as you can see there was LIMIT 0,4 which i was using to get 4 rows from database , is there anyway to get 4 rows with prepare statement'?

You need to loop through the results, using $stmt->fetch() as the condition, to retrieve the next row of results

while ($stmt->fetch()) {
        echo "$id - $title - $poster - $date<br />";
    }

 The variables $id, $title, $poster and $date will be populated from bind_result(), these shuould correspond with the fields from the SELECT statement

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.