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();
Related
This question already has answers here:
Call to a member function execute() on boolean in [duplicate]
(5 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 1 year ago.
I am trying to SELECT data from 1 database and insert into another, but I have problem in INSERT.
I get this error PHP Fatal error: Uncaught Error: Call to a member function execute() on boolean when I run my code.
MY CODE
<?php
include 'db_acc.php';
include 'db_sat.php';
$sql = "SELECT created_at,updated_at
FROM satellite1.show_activity";
$result=$conn1->query($sql);
while($row = $result->fetch_assoc()) {
echo "$row[updated_at]"; // I can get the value
$sql2 = "INSERT INTO analysis_account.currency SET
id=0,
currency_code='MYR',
currency_rate= 3.500,
currency_unit= 100,
base_currency= 1,
created_by= 2,
updated_by= 2,
created_at= '2021-05-17 14:10:32',
updated_at = ".$row["updated_at"].", // But I cant insert the value
curr_hidden = 1 ";
$query=$conn2->query($sql2);
}
$conn2->close();
?>
The problem is I can get the value of $row[updated_at] but I cant insert it into other database table, the connection is no problem because if I change $row[updated_at] into '2021-05-17 14:10:32' then the insert statement work.
I checked my connection, column names, symbols there are no problem of them. I really dont know what to do to solve the error.
Your INSERT query is missing quotes around the timestamp value. Just change
updated_at = ".$row["updated_at"].",
to
updated_at = '".$row["updated_at"]."',
This would fix the insert functionality but your code remains susceptible to SQL injection attacks. Please avoid simple string concatenation in your SQL queries and prefer prepared statements with placeholders.
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
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
(1 answer)
How to get more information from PDO failure "Error!: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined"?
(2 answers)
Closed 4 years ago.
Is something wrong with the syntax of this query? I'm using mysql and PHP.
$stmt = $conn->query("SELECT denominazione
FROM tab02_impianti
WHERE tab02_impianti.cod_impianto = tab05_workgroup.cod_impianto
AND tab05_workgroup.cod_utente = tab01_utenti.cod_utente ");
Because I get the error:
Fatal error: Uncaught Error: Call to a member function fetch() on boolean in...
The error show up because of the WHERE sentence. It works without it but I get all the data of the table. So this part: "SELECT denominazione FROM tab02_impianti" should be ok.
I cant figure it out, can I get some help please? Thank you very much :)
Let me write something more:
I have users table (tab01) and industrial installations (tab02). Every user can be related to more installations and vice versa.
The tab05, workgroup, contain the id of both. I just want to show to the user the information about the industrial installations that he is allowed to.
The error you get is caused by fetch() function because your query() function returns false.
The false boolean is return on your query() function because there is an error somewhere on it. To help you debugging your query, you can check for error case :
$stmt = $conn->query("SELECT denominazione FROM tab02_impianti WHERE tab02_impianti.cod_impianto = tab05_workgroup.cod_impianto AND tab05_workgroup.cod_utente = tab01_utenti.cod_utente ");
//error case
if(!$stmt)
{
die("Execute query error, because: ". print_r($conn->errorInfo(),true) );
}
//success case
else{
//continue flow
}
This question already has answers here:
PHP PDO - Bind table name? [duplicate]
(2 answers)
Can PHP PDO Statements accept the table or column name as parameter?
(8 answers)
Closed 4 years ago.
when I run the following:
$pdo = new PDO('sqlite:/path/to/database.db');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('CREATE TABLE IF NOT EXISTS :user (id INTEGER PRIMARY KEY AUTOINCREMENT, time INTEGER NOT NULL, ping TEXT NOT NULL);');
$user = "me";
$stmt->execute(array('user' => $user));
I get the following error, referencing the :user part of the SQL:
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 near ":user": syntax error in /datasql.php:21\nStack trace:\n#0 /datasql.php(21): PDO->prepare('CREATE TABLE IF...')\n#1 {main}\n thrown in /datasql.php on line 21
I can't find anything wrong with the SQL and if I run the command without the variable insertion, it runs fine.
What is causing this error?
Thanks ahead!
This question already has answers here:
Can PHP PDO Statements accept the table or column name as parameter?
(8 answers)
Closed 9 years ago.
$tconn = new PDO('mysql:host='.WW_HST.';dbname='.WW_DB, WW_USR, WW_PS);
$res = $tconn->prepare('SELECT * FROM :tbl');
$res->execute(array(':tbl'=>"ugb"));
When I use this code to draw data from the 'ugb' table, I get the following error:
'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ugb'' at line 1'
So it's correctly substituting :tbl for 'ugb' but whether I do a bind or just execute with an array, I always get an error. It works fine if I just do SELECT * FROM ugb though.
How can I correct this problem?
PDO does not allow you to set variables in FROM.
You only could add table name in query string.
I usually do by this way:
$allowedTables = array('first', 'second', 'third');
if(in_array($tblName, $allowedTables)) {
$$res = $tconn->prepare("SELECT * FROM $tblName");
}
I don't think that PDO will allow you to bind a parameter to the FROM statement. You could try manualy escaping the table name parameter and after that adding it to the query like this:
$table = "ugb";
$tconn = new PDO('mysql:host='.WW_HST.';dbname='.WW_DB, WW_USR, WW_PS);
$res = $tconn->prepare('SELECT * FROM '. $tconn->quote($table));
$res->execute();
Hope this helps.