ArshSingh Posted April 6, 2014 Share Posted April 6, 2014 (edited) 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 April 6, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/287567-bind-the-rows-from-database-with-mysqli-prepare/ Share on other sites More sharing options...
mac_gyver Posted April 6, 2014 Share Posted April 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287567-bind-the-rows-from-database-with-mysqli-prepare/#findComment-1475160 Share on other sites More sharing options...
ArshSingh Posted April 6, 2014 Author Share Posted April 6, 2014 (edited) 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 April 6, 2014 by ArshSingh Quote Link to comment https://forums.phpfreaks.com/topic/287567-bind-the-rows-from-database-with-mysqli-prepare/#findComment-1475161 Share on other sites More sharing options...
Solution mac_gyver Posted April 6, 2014 Solution Share Posted April 6, 2014 we can only help you with your actual code. posting anything else in a thread just sidetracks the problem. Quote Link to comment https://forums.phpfreaks.com/topic/287567-bind-the-rows-from-database-with-mysqli-prepare/#findComment-1475162 Share on other sites More sharing options...
ArshSingh Posted April 6, 2014 Author Share Posted April 6, 2014 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'? Quote Link to comment https://forums.phpfreaks.com/topic/287567-bind-the-rows-from-database-with-mysqli-prepare/#findComment-1475163 Share on other sites More sharing options...
Ch0cu3r Posted April 6, 2014 Share Posted April 6, 2014 (edited) 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 April 6, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287567-bind-the-rows-from-database-with-mysqli-prepare/#findComment-1475165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.