Jump to content

bind the rows from database with mysqli->prepare


Go to solution Solved by mac_gyver,

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();
}
?>
Edited by ArshSingh

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

Edited by ArshSingh

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

Edited by Ch0cu3r
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.