Jump to content

how to get all table records using prepared statements?

Recommended Posts

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.


	$_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)) {
		$array=  implode("|" , $row) ;
	  foreach( $data as $row ) {

		$id = $row['id'];
		$name =  $row['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 post
Share on other sites

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

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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