This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
For some reason I'm gettin this error on the second line of included code:
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 MySQL server version for the right syntax to use near 'div='CA' WHERE vid='400373'' at line 1' in /home/stretch045/public_html/scripts/auth.php:12 Stack trace: #0 /home/stretch045/public_html/scripts/auth.php(12): PDO->prepare('UPDATE users SE...') #1 /home/stretch045/public_html/index.php(35): Auth->checkToken('94257b73ea4ed51...') #2 {main} thrown in /home/stretch045/public_html/scripts/auth.php on line 12
code
$conn = $this->db;
$stmt = $conn->prepare("UPDATE users SET rating='".$xml->rating."', atc='".$xml->ratingatc."', pilot='".$xml->ratingpilot."', div='".$xml->division."' WHERE vid='".$xml->vid."'");
$stmt->execute();
if($stmt->rowCount()==0){
$stmt = $conn->prepare("INSERT INTO users (vid, fname, lname, rating, atc, pilot, div) VALUES (".$xml->vid.",".$xml->firstname.",".$xml->lastname.",".$xml->rating.",".$xml->ratingatc.",".$xml->ratingpilot.",".$xml->division.")");
$stmt->exec($stmt);
echo 'data inserted into db';
}
div is a reserved keyword in MySQL and needs to be escaped by backticks.
INSERT INTO users (vid, ..., `div`) VALUES (...)
Related
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);
This question already has answers here:
Can PHP PDO Statements accept the table or column name as parameter?
(8 answers)
Closed 7 years ago.
So I am quite new to using PDO and although I have to it to work using static settings I would like to be able to change the table name in a statement.
I had a look at this question here. and saw the highly up voted answer. So I attempted to recreate a simple version which would just dump all the information within a column.
Question: Why is my edited version of that persons example not work and return this error below.
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
MySQL server version for the right syntax to use near '' at line 1' in
C:\xampp\htdocs\ *****\database.php:84 Stack trace: #0
C:\xampp\htdocs\ *****\database.php(84): PDOStatement->execute() #1
C:\xampp\htdocs\ *****\database.php(88): buildQuery(1) #2 {main}
thrown in C:\xampp\htdocs\ *****\database.php on line 84
The code from the example linked with my small alterations.
function buildQuery( $get_var )
{
switch($get_var)
{
case 1:
$tbl = `r16.7`;
break;
}
global $db;
$sql = "SELECT * FROM $tbl";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 1);
var_dump($result);
}
buildQuery(1);
Do not confuse PHP and SQL.
You just used SQL quotes in PHP. While you have to use SQL quotes in SQL.
Linked answer is now fixed, so, I am closing this as a duplicate.
This question already has answers here:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDO [duplicate]
(4 answers)
Closed 8 years ago.
So here's my problem I get this error
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 MySQL server version for the right syntax to use near 'key) VALUES ('email#email' , '6b7d4d69e7595943da5bfb5723ceb3ef2e559275')' at line 1' in /Users/matt/Desktop/Likes/forgot/f.php on line 39
When trying to run this code
$gen = $con->prepare("INSERT INTO reset (user, key) VALUES (:user , :key)");
$gen->bindValue(':user', $username, PDO::PARAM_STR);
$gen->bindValue(':key', $token, PDO::PARAM_STR);
$gen->execute();
Any ideas? I'm binding both values so I'm not sure what's wrong. I've also went over and checked for syntax errors, but couldn't find any.
That's because key is a reserved word. You'll either need at add backticks or choose a different name. You can look at all the reserved words here. So this is what your final code should look like
$gen = $con->prepare("INSERT INTO reset (user, `key`) VALUES (:user , :key)");
$gen->bindValue(':user', $username, PDO::PARAM_STR);
$gen->bindValue(':key', $token, PDO::PARAM_STR);
$gen->execute();
This question already has answers here:
Select a column with a keyword name
(6 answers)
Closed 8 years ago.
I have this pre-existing table structure (yes data and time are terrible names but they are already there):
However everything grinds to a halt on updating the in/out field.
(Also tried with prepared statements: no luck)
This does not work:
$update_punch = $conn->query("UPDATE ttime SET date='$the_edited_date_w_mysql', time='$the_edited_time', inout='$the_ins_and_outs' WHERE id='$the_id' LIMIT 1");
The first line of this works but it chokes on the 2nd (chocks with inout being a var or 'in':
$update_punch = $conn->query("UPDATE ttime SET date='$the_edited_date_w_mysql', time='$the_edited_time' WHERE id='$the_id' LIMIT 1");
$update_punch = $conn->query("UPDATE ttime SET inout='$the_ins_and_outs' WHERE id='$the_id' LIMIT 1");
Here is there error:
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 MySQL server version for the right syntax to use near 'inout='out' WHERE id='171366' LIMIT 1' at line 1' in /site/updatepunchesbystore2.php:43 Stack trace: #0 /site/updatepunchesbystore2.php(43): PDO->query('UPDATE ttime SE...') #1 {main} thrown in /site/updatepunchesbystore2.php on line 43
I have fought with it for an hour and I'm stumped!
Any thoughts?
wrap the keywords in backticks
`inout`='$the_edited_time'
should work for you
Try this:
$qry= "UPDATE ttime SET `inout`='{$the_ins_and_outs}' WHERE id='$the_id'";
$update_punch = $conn->query($qry);
Im using PHP PDO to let my Android app talk to a MySQL database.
Here's my PHP file:
<?php
$pdo = new PDO("mysql:host=x;dbname=x", "x", "x");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO k_user_groep(group, user, rol) VALUES (?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($_GET['groupid'], $_GET['user'], $_GET['rol']));
?>
The table is designed as follows:
groupid references a unique index in other table,
user references a primary key in other table,
rol references nothing.
Directly in MySQL the following query works:
INSERT INTO `k_user_groep`(`group`, `user`, `rol`) VALUES ('1', 'test', 'v');
This is my call on the PHP file:
x.php?groupid=1&user=test&rol=v
It returns the following:
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 MySQL server version for the right syntax to use near 'group, user, rol) VALUES ('1', 'test', 'v')' at line 1' in x.php:7 Stack trace: #0 x.php(7): PDOStatement->execute(Array) #1 {main} thrown in x.php on line 7
Any advice?
group is a reserved word in mySQL.
It works in your second example because you're wrapping the column name in backticks.
Well it clearly is not the same query you're trying in your PDO code and in the MySQL client — you have all your identifiers quoted in the client, while none are quoted in the PDO code.