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

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

Related

MySql php pdo stored procedure PDOException: SQLSTATE[HY000] [duplicate]

This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
Hej.
I get this error message when execute my code? I post value "2020-12-19" $data_input_textfield = $_POST["date"]; from my form. cant figure out where 1989 comes from...
Message:
Exception has occurred.
PDOException: SQLSTATE[HY000]: General error: 1525 Incorrect DATE value: '1989'
The code:
$sql = "CALL booking_date_input($data_input_textfield)";
$stmt = $dbh->getInstance()->prepare($sql);
$stmt->execute();
$date=$stmt->fetch();
Best regards
/Svante
You can try a slightly different approach whereby you supply a placeholder to the sql command and execute the statement with the variable bound to that placehoolder - like so:
$sql = "CALL `booking_date_input`(:date);";
$stmt = $dbh->getInstance()->prepare($sql);
$stmt->execute(array(':date'=>$data_input_textfield));
$date=$stmt->fetch();

PDO fetchALL command not returning data [duplicate]

This question already has answers here:
Why does this PDO statement silently fail?
(2 answers)
Closed 2 years ago.
I am trying to return information from my database, but all I get is an empty array when I know the database table is not empty.
This is my code
$pdo_dbconn = new PDO("pgsql:host=localhost;dbname=null;user=null;password=null")
or die ("Could not connect");
$sql = $pdo_dbconn->prepare("SELECT * FROM prsnl_codes;");
$sql->execute();
$array = $sql->fetchALL();
var_dump($array);
This is what the response is
array(0) {}
Can anyone tell me what is going on?
I solved the issue, and all the code up there was correct. The problem I was having was permission on the database, which I resolved.

How to find a data by an another data which is known? [duplicate]

This question already has answers here:
Commands out of sync; you can't run this command now
(23 answers)
Closed 3 years ago.
I have 1 variable called $ip that is the IP address who entered my site.
and I want to find banned states from $ip in the SQL database.
I have used this code but didnt work:
$state = $mysqli->prepare('SELECT bannedstate FROM `unbanned` WHERE ip=?');
$state->bind_param("b", $bannedstate);
$state->execute();
$state->bind_result($Selectedbannedstate);
$state->fetch();
All help will be appreciated
You try to bind your $bannedstate to the parameter "b" which isn't in your statement.
I think it should be something like this
$state = $mysqli->prepare('SELECT bannedstate FROM `unbanned` WHERE ip=:b');
$state->bind_param(":b", $bannedstate);
$state->execute();
$state->bind_result($Selectedbannedstate);
$state->fetch();

Notice: Trying to get property of non-object after a modification to already working bit of code [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
So I am trying to work on this block of code for a personal project. This is an iteration of a sql query that is being passed by php.
1 $sql = "SELECT * FROM `table 1`";
2 $result = $conn->query($sql);
3
4 if ($result->num_rows > 0) {
5 // output data of each row
6 while($row = $result->fetch_assoc()) {
7 echo "id: " . $row["ID"]. " - Spell Name: " . $row["Spell Name"]. " -
8 School " . $row["S"]. "<br>";
9 }
10 } else {
11 echo "0 results";
12 }
13 $conn->close();
When I run it, it gives
Notice: Trying to get property of non-object on line
I simply pulled this from the w3school tutorial, and replace the relevant column names. Just for the record, the dataset I am working with has roughly entries
It would have helped to have the line number, but so far, it looks like $conn is not defined. You don't have a connection to the database.
The only line in that code where you're accessing a property is $result->num_rows on line 4, which suggests that $result is not an object.
Assuming $conn is a PDO connection, it means PDO::query() is returning false, since that is the only non-object value it can return. If the connection object itself was invalid, you'd see a fatal error from line 2 when you run the query.
In turn, this suggests that your SQL is not executing correctly. Since your query is a simple SELECT * FROM... statement, the likely reason is that table 1 is either not a table, or is not accessible. Check by running the query against your database directly, using the MySQL command-line or similar.

Excute big Mysql file with PDO? [duplicate]

This question already has answers here:
Slow performance MySql
(2 answers)
Closed 6 years ago.
i am buiding a automatic create website system...when i am installing web for my customer, at step i am excute the my sql file i am using like this :
# MySQL with PDO_MYSQL
$db = new PDO("mysql:host=$mysqlHostName;dbname=$mysqlDatabaseName", $mysqlUserName, $mysqlPassword);
$query = file_get_contents($local_sql); //Local file abc.sql
$stmt = $db->prepare($query);
if ($stmt->execute()){
My problem is i have up to 117 table need to create and many many insert sql on this file.
Everytime i buidling 2 or more than website it freeze my server and let it dead.
My question is have any solution ? can excute a big sql file like that faster the way i am using ?
Sorry if my english so bad.
Thank all !
Try to execute your queries one after another. Parse SQL file and execute queries in loop like this:
$queries = explode(';', $query);
foreach ($queries as $query) {
$stmt = $db->prepare($query);
$stmt->execute();
}

Categories