Balderick Posted November 12, 2016 Share Posted November 12, 2016 Desperately needed: a way to retrieve all records from a database table while using prepared statements. The script now works but only for 1 record. <?php $_POST['id']= '2'; // set connection variables // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $stmt = $conn->prepare("SELECT * FROM table WHERE id LIKE ? ;"); if ( !$stmt ) { yourErrorHandler($conn->error); // or $mysqli->error_list } else if ( !$stmt->bind_param('i', $id ) ) { yourErrorHandler($stmt->error); // or $stmt->error_list } else if ( !$stmt->execute() ) { yourErrorHandler($stmt->error); // or $stmt->error_list } else { $result = $stmt->get_result(); while ($row = mysqli_fetch_assoc($result)) { var_dump($row); $data[]=$row; var_dump($id); var_dump($data); $array= implode("|" , $row) ; var_dump($array); foreach( $data as $row ) { $id = $row['id']; $name = $row['name']; var_dump($id); var_dump($name); } } } ?> I hope someone can give a query that executes what is desired, but maybe there is also a possibilty to put the query in a loop? Please help. Link to comment Share on other sites More sharing options...
davej Posted November 12, 2016 Share Posted November 12, 2016 $stmt = $conn->prepare("SELECT * FROM table"); Link to comment Share on other sites More sharing options...
Balderick Posted November 13, 2016 Author Share Posted November 13, 2016 I tried that one, but the problem is that I get an error message. Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement the error is located in this line: else if ( !$stmt->bind_param('i', $id ) ) { I also tried to put in all columns but allegedly that's not the solution either. can you tell what is required to fetch all the data found in the table? Link to comment Share on other sites More sharing options...
Ingolme Posted November 13, 2016 Share Posted November 13, 2016 You don;t need bind_param, because your query has no parameters. 1 Link to comment Share on other sites More sharing options...
Balderick Posted November 14, 2016 Author Share Posted November 14, 2016 You don;t need bind_param, because your query has no parameters. Thanks, this worked for me. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now