Jump to content
Sign in to follow this  
Balderick

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.


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

 

 

Share this post


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?

Share this post


Link to post
Share on other sites

You don;t need bind_param, because your query has no parameters.

  • Like 1

Share this post


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.

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

Loading...
Sign in to follow this  

×
×
  • Create New...