PDO::ATTR_EMULATE_PREPARES breaks a simple command [duplicate] - php

This question already has answers here:
PHP PDOException: "SQLSTATE[HY093]: Invalid parameter number"
(4 answers)
How to view query error in PDO PHP
(5 answers)
Use bound parameter multiple times
(5 answers)
Closed 4 years ago.
I have this very simple MCVE where even no table/database is affected:
<?php
$pdoConnection = new PDO( "mysql:host=<hostname>", "<user>", "<pwd>" );
$pdoConnection->setAttribute( PDO::ATTR_EMULATE_PREPARES, TRUE );
$command = $pdoConnection->prepare("SELECT IF('1', -:foo, :foo) AS FOO");
$command->bindValue( "foo", 1, PDO::PARAM_INT );
$command->execute();
echo "<pre>";
print_r($command->fetch(PDO::FETCH_ASSOC));
?>
The result is as expected:
Array
(
[FOO] => -1
)
Disabling the prepare emulation with
$pdoConnection->setAttribute( PDO::ATTR_EMULATE_PREPARES, TRUE );
breaks totally the code. No error and also no output as nothing has been executed even it is a regular and correct MySQL code.
My questions:
Is there a legal reason why there is no result, or
is it a bug, but what causes the bug? PHP or MySQL?
Versions:
OS: SLES 12.3
PHP: 7.2.9
MySQL 8.0.12

Related

Problems with MYSQL IN Function PHP [duplicate]

This question already has answers here:
PHP - Using PDO with IN clause array
(9 answers)
Why does this PDO statement silently fail?
(2 answers)
Closed 3 years ago.
I have a little problem with my SQL query:
I have a variable ($arraySearch) with a string:
$arraySearch = "'Bob', 'Ross'";
My PHP query:
$stmt = $this->pdo->prepare("SELECT * FROM `kunden` WHERE `FAMNAME` IN (arraySearch =:arraySearch) AND `VORNAME` IN (arraySearch = :arraySearch)");
$stmt->execute(['arraySearch' => $arraySearch]);
$all = $stmt->fetchAll(PDO::FETCH_CLASS, $model);
Error Code:
Fatal error: Uncaught Error: Call to a member function execute() on boolean
I've been searching for the error for hours but I can't find it.
What am I missing?
Does somebody have any idea?
best regards

why is execute() PDO function return false? [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Why does this PDO statement silently fail?
(2 answers)
Closed 4 years ago.
public function updateTodo($iSequence, $aTodo)
{
//#TODO
$sTodo = $aTodo['todo'];
$stmt = $this->oConnection->prepare("UPDATE t_todolist
SET todo = $sTodo
WHERE sequence = $iSequence");
$stmt->execute();
}
Im doing a todo list for some reason i cant update my database because my $stmt is executing false.
If it returns false, then there is something wrong with your code. Make sure that you set the PDO properly. It would be better if you print the error message so that you know what the message is.
Try to put this code so that you will know what the error is
$stmt->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

PDO fetchAll always returns an empty array [duplicate]

This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 6 years ago.
I get an empty array when running:
try {
$pdo = new PDO('mysql:127.0.0.1:dbname=mytodo', 'root', 'root');
}
catch (PDOException $e) {
die('Could not connect.');
}
$statement = $pdo->prepare('select * from todos');
$statement->execute();
var_dump($statement->fetchAll());
I've checked the database and running the same query 'select * from todos' returns the results as expected.
I've tried using different databases and tables. I always get an empty array.
Any ideas as to what's going wrong?
I'm running MAMP PRO and get the same issue whatever PHP version I choose.
Any answers or pointers greatly appreciated
By default PDO will die silently on a lot of query errors. Try to check for typo errors also.
How to view query error in PDO PHP
// The rest of the statement can also go into the try block. And why not echo the $e error msg if you have one?

Change MySQL PDO connection option between queries [duplicate]

This question already has an answer here:
PDO Setting PDO::MYSQL_ATTR_FOUND_ROWS fails
(1 answer)
Closed 6 years ago.
I want to change the value of the PDO MYSQL_ATTR_FOUND_ROWS connection option between queries.
Initially I define the connection handle like:
$dbh = new PDO('mysql:host=localhost;dbname=db', $uid, $pwd, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
I would like to change the value of MYSQL_ATTR_FOUND_ROWS to false at some point in the program. Is that possible? If so, how is it done?
Try using this command:
$dbh->setAttribute("PDO::MYSQL_ATTR_FOUND_ROWS", true);
setAttribute() documentation

Commands out of sync when using mysqli and prepared statements [duplicate]

This question already has answers here:
Commands out of sync; you can't run this command now
(23 answers)
Closed 2 years ago.
I am trying to do some calls, but the 2nd query fails with command 'Commands out of sync; you can't run this command now' error.
The code looks like this:
$sql="call listReport();";
$results = mysqli_query($link,$sql);
$arr=array();
while($row=mysqli_fetch_array($results)) {
array_push($arr,$row);
}
mysqli_free_result($results);
// then I have this
// but this fails, giving the above mentioned error
$stmt = #mysqli_prepare($link,"select ........") or die(mysqli_error($link));
#mysqli_stmt_bind_param($stmt, 's', $s);
#mysqli_stmt_execute($stmt);
#mysqli_stmt_bind_result($stmt, $s);
#mysqli_stmt_fetch($stmt);
#mysqli_stmt_close($stmt);
I actually used mysqli_free_result($results); but doesn't worked. What do I miss?
The problem is that mysql stored procedures can return various result sets, so you should use mysqli_multiquery

Categories