Jump to content

how to get all table records using prepared statements?


Balderick

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.

 

 

Link to comment
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 comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...