PDOException in MySQL 5.7 - php

I'm getting:
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 'LIMIT 1' at
line 1'
I'm trying to upgrade some code from MySQL 5.6 to 5.7 and I'm not sure how to rewrite this?
public function getPlayerInfo($uid){
$this->session->newQuery();
$sqlSelect = "SELECT COUNT(*) AS total, login, gameIP, homeIP, gamePass, email FROM users WHERE id = $uid LIMIT 1";
$data = $this->pdo->query($sqlSelect)->fetch(PDO::FETCH_OBJ);
if($data->total == 0){
exit();
}
To clarify what this does, it's supposed to return relevant player data, for example, further down in this file, I have:
if($doomStats['DOOM']['clanID'] == 0){
$doomedBy = ''.self::getPlayerInfo($doomStats['DOOM']['creatorID'])->login.'';
} else {
$clan = new Clan();
$clanInfo = $clan->getClanInfo($doomStats['DOOM']['clanID']);
$doomedBy = ''.$clanInfo->name.'';
$doomedBy .= ' <span class="small nomargin">(Released by '.self::getPlayerInfo($doomStats['DOOM']['creatorID'])->login.')</span>';
}
I hope this clarifies.

using count without an aggregate function? eg: groupby also put $uid in single quotes

Related

SQLSTATE[42000]: Syntax error or access violation: 1064 Error

I am getting the following error on a website. I create ticket for this reason in my hosting provider. It told me "You need to edit the select query, not a select query suitable for the mariadb version on the server." they said.
error_log File:
[25-Dec-2021 19:50:24 Europe] PHP Fatal error: Uncaught PDOException: 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 'and dripfeed= 2' at line 1 in /home/user/public_html/script.php:461
Stack trace:
#0 /home/user/public_html/script.php(461): PDO->query('SELECT * FROM o...')
#1 /home/user/public_html/index.php(35): require_once('/home/user/...')
#2 {main}
thrown in /home/user/public_html/script.php on line 461
script.php File:
$dripfeedvarmi = $conn->query("SELECT * FROM demo WHERE user=$user_id and dripfeed=2");
if ($dripfeedvarmi->rowCount())
{
$dripfeedcount = 1;
}
else
{
$dripfeedcount = 0;
}
Current DB Version: 10.2.41-MariaDB-cll-lve
PHP Version: 7.4.25
OS: Linux
Thank you in advance for your help.
even if the MySQL syntax is correct, do not write code like this. Always prepare your query to make it secure!
Try this example:
$query = 'SELECT * FROM demo WHERE user = ? AND dripfeed = ?';
$array = array($user_id, 2);
$init = $conn->prepare($query);
$init->execute($array);
$rowCount = $init->rowCount();
if($rowCount > 0){
$dripfeedcount = 1;
}else{
$dripfeedcount = 0;
};
Also if you are storing the id of the user, so why the column name is not user_id instead of user? Be clean...
You can also try like this to execute the query using prepare() and execute() methods.
$dripfeedvarmi = $conn->prepare("SELECT * FROM demo WHERE user=:user and dripfeed=:dripfeed");
$dripfeedvarmi->execute([':user'=>$user_id,':dripfeed'=>2]);
if ($dripfeedvarmi->rowCount()>0)
{
$dripfeedcount = 1;
}
else
{
$dripfeedcount = 0;
}

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

I'm trying to do a form to insert values on a data base, but it's not working.
In fact, I used to use a VM that is now dead. And when I switched to Xammp my program didn't work anymore.
$titre = $_POST["titre"];
$categorie = $_POST["categorie"];
$portion = $_POST["portion"];
$heure_cuiss = $_POST["heure_cuiss"];
$minute_cuiss = $_POST["minute_cuiss"];
$heure_prepa = $_POST["heure_prepa"];
$minute_prepa = $_POST["minute_prepa"];
$heure_rep = $_POST["heure_rep"];
$minute_rep = $_POST["minute_rep"];
$cuiss = $_POST["cuiss"];
$cost = $_POST["cost"];
$dif = $_POST["dif"];
$histoire = $_POST["histoire"];
$region = $_POST["region"];
$temps = intval($heure_cuiss) + intval($minute_cuiss)/60 + intval($heure_prepa) + intval($minute_prepa)/60 + intval($heure_rep) + intval($minute_rep)/60;
$query = $bdd -> prepare('INSERT INTO recette (titre, categorie, portion, heure_cuiss, minute_cuiss, heure_prepa, minute_prepa, heure_rep, minute_rep , cuiss, cost, dif, histoire, region, temps)
VALUES(:titre, :categorie, :portion, :heure_cuiss, :minute_cuiss, :heure_prepa, :minute_prepa, :heure_rep, :minute_rep, :cuiss, :cost, :dif, :histoire, :region, :temps)');
$query -> execute(array('titre'=>$titre, 'categorie'=>$categorie, 'portion'=>$portion, 'heure_cuiss'=>$heure_cuiss, 'minute_cuiss'=>$minute_cuiss, 'heure_prepa'=>$heure_prepa, 'minute_prepa'=>$minute_prepa, 'heure_rep'=>$heure_rep, 'minute_rep'=>$minute_rep, 'cuiss'=>$cuiss, 'cost'=>$cost, 'dif'=>$dif, 'histoire'=>$histoire, 'region'=>$region, 'temps'=>intval($temps)));
I get this error
Fatal error: Uncaught PDOException: 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 'portion, heure_cuiss, minute_cuiss, heure_prepa, minute_prepa, heure_rep, min...' at line 1 in C:\xampp\htdocs\ptut\upload\back-index.php:46 Stack trace: #0 C:\xampp\htdocs\ptut\upload\back-index.php(46): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\ptut\upload\back-index.php on line 46
I've tried to rewrite my database, to write my insert with '?' but nothing works.
I've been working on this problem for 5 hours. I really need your help !
Thanks, Thomas
Make sure your password is empty like this :
$bdd = new PDO('mysql:host=localhost;dbname=yourDataBase', 'root', '');

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

Hello everyone I try to update to boolean in my table, so I had Error PDOException and I don't Why :
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax
My line code sql is this :
public function updateSignal(Comments $comment)
{
$req = 'UPDATE Comments SET signal = TRUE WHERE idComments = :comment';
$result = $this->getBdd()->prepare($req);
$result->bindValue(':comment', $comment->getIdComments());
return $result->execute();
}
I don't find where is my error syntax, Please need help Thanks
SIGNAL is a reserved word. It's best to avoid it, but you can use it if you wrap it in backticks.
$req = 'UPDATE Comments SET `signal` = TRUE WHERE idComments = :comment';
(MySQL reserved words)

Failed to Run Query: Exception in Connection.php line 673: SQLSTATE[42000]: Syntax error or access violation: 1064

I'm building a laravel application where there is a controller which functionality is we have to book a classroom where if a time has already booked or there in database we can't book a classroom in that time interval. I have used the following controller :
public function postAllocateRoom(Request $request)
{
$classRoom = new ClassRoom();
$classRoom->department_id=$request->Input(['department_id']);
$classRoom->room_id=$request->Input(['room_id']);
$classRoom->course_id=$request->Input(['course_id']);
$classRoom->day_id=$request->Input(['day_id']);
$classRoom->start=$request->Input(['start']);
$classRoom->end=$request->Input(['end']);
$startTime = Carbon::parse($request->input('start'));
$endTime = Carbon::parse($request->input('end'));
$classRoom=DB::select('SELECT allocate_rooms.id
FROM allocate_rooms
WHERE '.$startTime.' BETWEEN allocate_rooms.start AND allocate_rooms.end')->count();
$messages ="Class Room Already Taken";
if ($classRoom>0) {
return redirect('allocateRoomPage');
}
else {
$classRoom->save();
return redirect('allocateRoomPage');
}
}
But I'm getting following Error while saving in database:
QueryException in Connection.php line 673: 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 '17:00:00 BETWEEN allocate_rooms.start
AND allocate_rooms.end' at line 3 (SQL: SELECT allocate_rooms.id FROM
allocate_rooms WHERE 2016-05-08 17:00:00 BETWEEN allocate_rooms.start
AND allocate_rooms.end)
How do I solve this?
The date needs to be quoted as well:
$classRoom=DB::select('SELECT allocate_rooms.id
FROM allocate_rooms
WHERE "' . $startTime . '" BETWEEN allocate_rooms.start AND allocate_rooms.end')->count();
I would suggest searching for a method to prepare MySQL queries in laravel, instead of writing them by hand.
Go through this blog post regarding the same: http://fideloper.com/laravel-raw-queries

fatal error in php and mysql

i am having a problem with my script in php/mysql. here is the error displayed by the server:
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 'if exists (select * from notificacoes where uid in () order by id desc' at line 1' in C:\wamp\www\bigui\classes\Notificacoes.class.php on line 57
and here is my php code:
static function listar(){
$strIdAmigos = Amizade::$strIdAmigos;
$query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc');
return $query->fetchAll(PDO::FETCH_ASSOC);
}
my table in the mysql is empty, with no values. when i insert a value in it, the error goes away and everything is fine. any help?
If $strIdAmigos is empty, it causes syntax errors.
Before you execute this query, you should check the $strIdAmigos value whether it's empty or not to avoid this issue. Not to forget to escape the values if needed.
When you run your query with nothing in the variable $strIdAmigos, it will error out.
Try initializing and/or checking your variable, $strIdAmigos, before running your query:
$strIdAmigos = "";
if (empty($strIdAmigos)) {
/* Uh oh, throw an error */
} else {
$query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc');
}
Note that if $strIdAmigos = "0" , the empty($strIdAmigos) will still evaluate to true and, hence, will NOT run the query.

Categories