Answers for "how to use prepare statement correct with get_result"

0

how to use prepare statement correct with get_result

<?php
//maybe this came from previous page of yours(ex:-in validation)
$column_name_value_wanted_to_fetch = "something";

//create a connection
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

//create a template
$query = "SELECT * FROM table_name WHERE column_name = ?";

//prepare the statement
$stmt = $mysqli->prepare($query);

//bind parameters
/*if you already know the value; you can do as follows, or use variable down here 
and assign value after this before executing it*/
$stmt->bind_param("s", $column_name_value_wanted_to_fetch);

//execute the statement; this will return true is success and false on failure
//therefore we can use this as a condition to proceed like this; if($stmt->execute()){}
$stmt->execute();

/*Retrieves a result set from a prepared statement as a mysqli_result object
 This function cannot be used together with mysqli_stmt_store_result().
 Both of these functions retrieve the full result set from the MySQL server.*/
//visit https://www.php.net/manual/en/class.mysqli-result.php to check all methods
$result = $stmt->get_result();

//optional
//to get a count of results you have
$count = $result->num_rows

//this will result an associative array
while ($row = $result->fetch_assoc()) {
	//now you can get details of results you fetched
}
Posted by: Guest on August-14-2021

Code answers related to "how to use prepare statement correct with get_result"

Browse Popular Code Answers by Language