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
This question already has answers here:
Commands out of sync; you can't run this command now
(23 answers)
Closed 4 years ago.
I normally read the data from the database, but I get an error when using the while.
My code is:
$BarberId = 1;
$stmt = $db->prepare("CALL `GetBranch`(?);");
$stmt->bind_param('i', $BarberId);
$stmt->execute();
$Tree_Barber_Id = NULL;
$stmt->bind_result($Tree_Barber_Id);
$stmt->store_result();
if($stmt->num_rows)
{
while($stmt->fetch())
{
$Priod = NULL;
$stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");
$stmt2->bind_param('i', $Tree_Barber_Id); //ERROR IS HERE!!!
$stmt2->execute();
$stmt2->bind_result($Priod);
$stmt2->store_result();
}
}
$stmt->close();
I think the error was caused because the variable stmt has not been closed yet. But by closing that by $stmt->close(); while command will not work.
Error is :
Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17
Read the error message.
Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\file1.php on line 17
Line 17 is this:
$stmt2->bind_param('i', $Tree_Barber_Id);
The error message is telling you $stmt2 is a boolean. $stmt2 comes from this line:
$stmt2 = $db->prepare("SELECT `priod` FROM `t_barber` WHERE `id`=?");
mysqli's prepare() function returns false if there was an error.
This means your query is invalid. You can find out what's invalid about it by looking at $db->error
This question already has answers here:
php mysqli query expects parameter 1 be to string
(2 answers)
Closed 6 years ago.
I am getting this error:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs
My code:
function dbQuery($sql)
{
$result = mysqli_query($sql) or die(mysqli_connect_error());
return $result;
}
It's inside a function, MySQLi is not having access to your connection as far as I know.
If your database connection is named $conn, you can write it like:
function dbQuery($sql) {
global $conn;
$result = mysqli_query($conn, $sql) or die(mysqli_connect_error());
return $result;
}
Please read the manual on how to use mysqli_query: http://php.net/manual/en/mysqli.query.php. The correct way to use it is:
$result = mysqli_query($conn,$sql);
where $conn is your mysqli connection, and $sql is the query to run.
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 months ago.
I've been stuck on this error , please help me this is my code
PHP Fatal error: Call to a member function bind_param()
$statement= $db->prepare("insert into uploaddetails(idnum,title,desc,author,tags,title) values(?,?,?,?,?,?)");
$id='NULL';
$title=$_POST['title'];
$description=$_POST['description'];
$author=$_POST['author'];
$tags=$_POST['tags'];
$file= basename($_FILES["fileToUpload"]["name"]);
$statement->bind_param( 'isssss', $id,$title, $description,$author,$tags,$file);
$statement->execute();
$db->close();
$statement->close();
Since nobody else has spotted the issue, I'll post it for you. The reason you're prepare() is failing is because you're trying to use a MySQL Reserved Word. The word desc is a reserved word in MYSQL, which means you need to wrap it in backticks like this:
$statement= $db->prepare("insert into uploaddetails(idnum,title,`desc`,author,tags,file) values(?,?,?,?,?,?)");
It also helps to use proper practice when inserting into a database/using prepared statements.
$statement= $db->prepare("insert into uploaddetails(idnum,title,`desc`,author,tags,title) values(?,?,?,?,?,?)");
if($statement !== FALSE) {
// do the binds...etc
}
Notes
file is also a reserved word, I don't know what your actual file columns name is, so keep that in mind.
Your prepare statement is failing because of the query, what you need to do is to make sure the statement is not false in order to execute bind_param, otherwise view the prepare query error as follows :
//Make sure the statement is not false
if($statement !== FALSE)
{
$statement->bind_param( 'isssss', $id,$title, $description,$author,$tags,$file);
$statement->execute();
$db->close();
$statement->close();
}
//Otherwise check why the prepare statement failed
else
{
die('prepare() failed: ' . htmlspecialchars($db->error));
}
Try this. your code is modified.
$statement= $db->prepare("INSERT INTO uploaddetails (title,desc,author,tags,file) VALUES(?,?,?,?,?)");
//$id='NULL';
$title=$_POST['title'];
$description=$_POST['description'];
$author=$_POST['author'];
$tags=$_POST['tags'];
$file= $_FILES["fileToUpload"]["name"];
$statement->bind_param( 'isssss',$title, $description,$author,$tags,$file);
$statement->execute();
$db->close();
$statement->close();
//---- Move the file to desired location...
-ID is not required because it is auto increment and mysql will take care of it,
-and you had wrong field name for file, which was title and I change it to file(correct it if you have any other name instead).
possible errors
1)column count in the table is different from your query.
2)although it shows the error in the bind_param line, the error may occur in the prepare statement line(in your case line 1)
3)you can put echo statement before and after these lines and caught the error
(in my case I repeated the same field name twice in the prepared statement)
fetch following code with your requirements and tryout
$stmt = $conn->prepare("INSERT INTO SalesReturn(CRDNUMBER, CRDDATE, REFERENCE,CUSTOMER,ITEM,QTYRETURN,UNITPRICE,TIAMOUNT1,TIAMOUNT2,EXTCRDMISC,TAMOUNT1,TAMOUNT2,CRDSUBTOT,CRDNET,CRDETAXTOT,CRDNETNOTX,CRDNETWTX,TransactionType) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
echo "after prepare";
$stmt->bind_param("ssssssssssssssssss",$CRDNUMBER,$CRDDATE,$REFERENCE,$CUSTOMER,$ITEM,$QTYRETURN,$UNITPRICE,$TIAMOUNT1,$TIAMOUNT2,$EXTCRDMISC,$TAMOUNT1,$TAMOUNT2,$CRDSUBTOT,$CRDNET,$CRDETAXTOT,$CRDNETNOTX,$CRDNETWTX,$TransactionType);
echo "after bind_param statement";
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'm creating quite big import script. I want to see any problematic (unexecuted) SQL queries. I have problem with catching wrong SQL queries with try-catch PHP block.
I have a query:
SELECT id FROM tag WHERE name IN ()
Of course there is an error in it so I want to print queries like that with this code:
$sql = "SELECT id FROM tag WHERE name ".$tagsSql."";
try
{
$query = mysqli_query($this->mysqli, $sql);
$result = $query->fetch_assoc();
}
catch(Exception $e)
{
echo 'Problem with: '.$sql;
print_r($e); die;
}
When running the script PHP just throws me this:
Fatal error: Call to a member function fetch_assoc() on a non-object in C:\www\blackboard-import\index.php on line 227
Why this error isn't catched? Because it's fatal? How can I handle that kind of situations?
I use mysqli to contact with MySQL.
This is no regular exception but a fatal error (which causes PHP to shut down). mysqli_query() will return false on error, this is why your current script is failing, so a solution could be something like the following
try {
$query = $this->msqli->query($sql);
if ($query === FALSE) {
throw new Exception($this->mysqli->error);
}
$result = $query->fetch_assoc();
} catch(Exception $e) {
//...
}