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!
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:
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();
This question already has answers here:
Reference - What does this error mean in PHP?
(38 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 have been trying to do a DELETE statement and i even tried running the query in MySQL workbench and everything was ok but in my service it does't seem to accept it.
$statement_delete = $this->conn->prepare("DELETE FROM crm_suite_tcl.aos_products_quotes where parent_id = ?");
$statement_delete->bind_param("s", $id_pedido);
$statement_delete->execute();
$resultado_borrar_lineas = $statement_delete->get_result();
It throws that the statement is false, so it doesn't let me execute it
Fatal error: Call to a member function bind_param() on boolean in
Fatal error: Call to a member function bind_param() on boolean in C:\xampp2\htdocs\crm\service\sync\include\db_handler.php on line 210
The error you are getting basically means that your statement cannot be prepared most likely due to a syntax error (a column or table that doesn't exist).
Try adding in your code:
if(!$statement_delete){
error_log($this->conn->error);
}
This should write the error to the console and you will be able to correct it.
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:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 4 years ago.
I wrote the query system with android studio and PHP.
My PHP code is as follows.Opened with a web browser but an error occurred.The error message is as follows
Notice: Undefined index: searchQuery in C:\xampp\htdocs\client\beetle_search.php on line 5
Fatal error: Uncaught exception '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
MariaDB server version for the right syntax to use near 'table 1 where
MATCH(name,scientific_name) AGAINST(NULL)' at line 1' in
C:\xampp\htdocs\client\beetle_search.php:9 Stack trace: #0
C:\xampp\htdocs\client\beetle_search.php(9): PDOStatement->execute()
1 {main} thrown in C:\xampp\htdocs\client\beetle_search.php on line 9
require_once('config.inc.php');
$search_query=$_POST['searchQuery'];
$sql = 'SELECT * from table 1 where MATCH(name,scientific_name) AGAINST(:search_query)';
$statement = $connection->prepare($sql);
$statement->bindParam(':search_query', $search_query, PDO::PARAM_STR);
$statement->execute();
if($statement->rowCount())
{
$row_all = $statement->fetchall(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($row_all);
}
elseif(!$statement->rowCount())
{
echo "no rows";
}
Where did you write it wrong? thanks
You should add a table name in your query. Update your query
from
$sql = 'SELECT * from table 1 where MATCH(name,scientific_name) AGAINST(:search_query)';
to
$sql = 'SELECT * from table_name where MATCH(name,scientific_name) AGAINST(:search_query)';
Also, in order to use MATCH function, you must have the full text index on the column. Otherwise, you will get an error Can't find FULLTEXT index matching the column list'. The solution of this problem is given below...
Assuming you are using MyISAM engine, Execute:
ALTER TABLE table_name ADD FULLTEXT(name);
ALTER TABLE table_name ADD FULLTEXT(scientific_name);