Fetch data failed [duplicate] - php

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 1 year ago.
I want to fetch data with the prepared statements but getting the following answer. I also tried to follow previous solved question related to this which was given on StackOverflow but failed. Help me to solve the issue.
Fatal error: Uncaught Error: Call to a member function execute() on bool
<?php
$stmt = $mysqli->prepare("SELECT id,addpage,post_name FROM detail WHERE id = ? ");
$stmt->execute();
$result = $stmt->get_result();
var_dump($row = $result->fetch_all(MYSQLI_ASSOC));
echo $row['id'];
echo $row['addpage'];
echo $row['post_name'];
?>

mysqli_prepare returns bool (false) on error, in this case likely because you forgot to add the parameters.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $mysqli->prepare("SELECT id,addpage,post_name FROM detail WHERE id = ? ");
$stmt->bind_param('id', $some_defined_id);
$stmt->execute();
$result = $stmt->get_result();
var_dump($row = $result->fetch_all(MYSQLI_ASSOC));
echo $row['id'];
echo $row['addpage'];
echo $row['post_name'];
Also, see the documentation at https://www.php.net/manual/en/mysqli.prepare.php

Related

Does anyone know why am I getting a fetch_array() boolean error? [duplicate]

This question already has answers here:
How to fetch all in assoc array from a prepared statement?
(4 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am trying to fetch TaxRate data from a table and I'm trying to select the first array only. However, I keep getting this error
"Fatal error: Uncaught Error: Call to a member function fetch_array()
on boolean in C:\xampp\htdocs\flowerique\shoppingCart.php:77 Stack
trace: #0 {main} thrown in C:\xampp\htdocs\flowerique\shoppingCart.php
on line 77"
Does anyone know the cause of this error and how do I fix it? Thanks!
This is my code :
//Retrieve Current GST Pricing
$qry = "SELECT * FROM gst GROUP BY EffectiveDate DESC";
$stmt = $conn->prepare($qry);
$stmt->execute();
//$stmt->close();
$result = $conn->query($qry);
$row = $result->fetch_array(); // This is line 77
while($row["EffectiveDate"] < date("Y-m-d"))
{
$row = $result->fetch_array();
}
$currentTaxRate = $row["TaxRate"];

Notice: Trying to get property 'num_rows' of non-object in [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
<?php
$queryuser = "SELECT id, email, firstname, lastname, address, city, country FROM users WHERE role = 'client'";
$resultuser = $connection->query($queryuser);
if ($resultuser->num_rows > 0)
{
// output data of each row
while ($rowuser = $resultuser->fetch_assoc())
{
$id_user = $rowuser['id'];
$firstname = $rowuser['firstname'];
$lasttname = $rowuser['lastname'];
$email = $rowuser['email'];
$phone = $rowuser['phone'];
$city = $rowuser['city'];
$state = $rowuser['state'];
$address = $rowuser['address'];
?>
Perhaps you've copied code from this page https://www.w3schools.com/php/php_mysql_select.asp
Please understand code line by line first. You have not define $connection variable in your code (may be it is a intentional approach for security).
Your notice saying that you are trying to get num_rows property from the object $resultuser but in this query $resultuser is not a object due to any error/faults of your query because query() returns FALSE on failure and object on success.
To confirm, you can check your result set by using var_dump();
Please replace this line if $resultuser->num_rows > 0 with (!empty($resultuser->num_rows) && ($resultuser->num_rows > 0)))

Unable to run query using prepared statement in MySQLi [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I am working on the below code. Why am I not able to run the query properly? I already check the database connection and it is fine
<?php
$sql = "SELECT dt, events, eventtype FROM events";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$stmt->bind_result($dt,$events,$eventtype);
$stmt->store_result();
if($stmt->num_rows >0) {
$stmt->fetch();
}
else {
echo "Cant Find The data!";
}
$stmt->close();
$mysqli->close();
echo $dt;
echo $events;
echo $eventtype;
?>
getting this error
Fatal error : Call to a member function execute() on boolean in
/srv/disk1/2555378/www/domain.net/index.php on line 113
This means that the variable $mysqli contains a boolean value, probably false.
According to the php docs, http://php.net/manual/en/mysqli.prepare.php, the function mysqli::prepare will return false in case of an error.
You should use the error variable to get more information, like here: http://php.net/manual/en/mysqli.error.php

PHP bind_param not binding parameter [duplicate]

This question already has answers here:
MySQLI Prepared Statement: num_rows & fetch_assoc
(5 answers)
Closed 5 years ago.
I am trying to search a table for specific items using a prepared statement in PHP. I am getting no errors, but also getting no record. Here is my code:
$items = [];
$search = "john";
if ($stmt = $this->con->prepare("SELECT * FROM phptest WHERE search = ?")) { //'john'";
$stmt->bind_param("s",$search);
$stmt->execute();
while ($row = mysqli_fetch_array($stmt)) {
$item = [];
$item['id'] = $row['id'];
$item['first'] = $row['search'];
$item['last'] = $row['data'];
array_push($items, $item);
}
}
return $items;
Now, when I don't use a prepared statement, and just SELECT * FROM phptest I get all the results in the table (including the item where search = 'john'). Furthermore, if I use the query SELECT * FROM phptest WHERE search = 'john' I get the one record where search = 'john'
But as soon as I turn it into the prepared statement, I get zero errors but zero records. I do get a warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
Which made me think my bind_param or execute() was returning FALSE, but when I check, it does not appear to be returning false.
I started off my adventure working through the tutorial https://www.simplifiedcoding.net/android-mysql-tutorial-to-perform-basic-crud-operation/, which I thought I understood fully but ran into my error when trying to make my own PHP API.
I then went to the manual http://php.net/manual/fr/mysqli.prepare.php, but still cannot find my error.
Though it has been closed as "off-topic," I have reviewed PHP bind_param not working and found nothing applicable to my situation.
Likewise, I am not finding the error in PHP bind_param not defined nor php bind_param is not working.
You're very close. mysqli_fetch_array() expects to be passed a result object, not the statement object itself:
$stmt = $conn->prepare(...);
$stmt->bind_param(...);
$stmt->execute();
$result = $stmt->get_result();
while ($row = mysqli_fetch_array($result)) {
Or, in the fully OO manner:
while ($row = $result->fetch_array()) {

Update PHP prepare bindparam [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 6 years ago.
This is my PHP code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$UpdateChecklist = 'UPDATE checklists SET ADMIN_ID=?, COMPUTER_ID=? WHERE id=?';
$stmtChecklist = $connection->prepare($UpdateChecklist);
$stmtChecklist->bind_param('ii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID']);
$isUpdate = $stmtChecklist->execute();
$lastUpdateId = mysqli_insert_id($connection);
$stmtChecklist->close();
$UpdateInstalledProgram = 'UPDATE checklist_programs SET CHECKLIST_ID=?, PROGRAM_ID=? WHERE id = ?';
$stmtProgramId = $connection->prepare($UpdateInstalledProgram);
$stmtProgramId->bind_param('ii', $lastUpdateId, $programId);
foreach ($_POST['PROGRAM_ID'] as $program) {
$programId = $program;
$stmtProgramId->execute();
}
$connection->close();
if ($isUpdate) {
header('Location: OverViewCheckList.php');
exit(0);
}
}
?>
and, I got some error which I don't know how to fix it:
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\checklist\updateChecklist.php on line 34
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in C:\xampp\htdocs\checklist\updateChecklist.php on line 44
Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\checklist\updateChecklist.php on line 57
Fatal error: Call to a member function fetch_assoc() on null in C:\xampp\htdocs\checklist\updateChecklist.php on line 59
In your both of your queries you are using 3 ?'s but only binding 2 variables.
Query 1:
$UpdateChecklist = 'UPDATE checklists SET ADMIN_ID=?, COMPUTER_ID=? WHERE id=?';
$stmtChecklist = $connection->prepare($UpdateChecklist);
$stmtChecklist->bind_param('ii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID']); /* Here*/
Query 2:
$UpdateInstalledProgram = 'UPDATE checklist_programs
SET CHECKLIST_ID=?, PROGRAM_ID=? WHERE id = ?';
$stmtProgramId = $connection->prepare($UpdateInstalledProgram);
$stmtProgramId->bind_param('ii', $lastUpdateId, $programId); /* Here */
You need to add a third variable e.g.:
$stmtChecklist->bind_param('iii', $_POST['ADMIN_ID'], $_POST['COMPUTER_ID'], $id3);
$stmtProgramId->bind_param('iii', $lastUpdateId, $programId, $id3);

Categories