MYSQL where clause causes syntax error - mysql

I just cannot find what is wrong with this simple statement:
$stat_qry = mysql_query("SELECT * FROM stats WHERE group=$galgroup") or die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);
i just get: "STATS ERROR: 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=1' at line 1"
i cannot get it to work with the 'where' clause, if i remove 'where' it works but just lists everything in the database

GROUP is a reserved word so it needs to be between back tick ``
Also, if $galgroup can be a string and not only a number, you need to add quotes arround it :
$stat_qry = mysql_query("SELECT * FROM stats WHERE `group`='$galgroup'") or
die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);

Related

Laravel Field Syntax Returns error or access Violation

There seems to be an identical question here, however, the context of that question is based around MariaDB and I am using MySQL. I'm not sure if this is related code or a server configuration?
When I try to query JSON data in Laravel 5.4...
$dataCheck = DB::table('station_data')
->select('entry_data')
->where('entry_data->status', '=', $data)
->where('station_id', '=', $stationID)
->first();
I get the following error...
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 '>'$."status"' =
? and station_id = ? limit 1' at line 1 (SQL: select entry_data
from station_data where entry_data->'$."status"' = test and
station_id = 7 limit 1)
I'm not sure why it's adding the $ symbol and messing with the syntax

Syntax error on SHOW TABLES LIKE

I don't understand why is it saying 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 ''User_notifications'' on this query:
<?php
include 'constants.php';
$username=$_POST['username'];
$notiftable=$username.'_notifications';
$con=new mysqli('',databaseuser,databasepassword,database);
if($con)
{
$q="SHOW TABLES LIKE '$notiftable'";
Your table name User_notifications is getting double-escaped (i.e. it is being escaped twice). This is most likely happening because the PHP function is escaping it already, and you are doing it a second time. Try not escaping the table name yourself, i.e.:
$q = "SHOW TABLES LIKE $notiftable";

Rails query boolean MySQL error

Doing any one of these:
notifications.where("read = 'true'")
notifications.where("read = '1'")
notifications.where("read = true")
notifications.where("read = 1")
results in the error:
Mysql2::Error: 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 'read = 'true')
However, this works just fine:
notifications.where(:read => true)
Any idea why this could be?
read is mysql reserved keyword you need to use back-ticks around your column name
notifications.where(" `read` = true")
Not familiar with ruby but you can refer to this answer to enclose the column with back-ticks
Mysql Reserved Words

PDO basic select query

I'm learning PDO and try to make a query:
$stmt = $conn->prepare("SELECT * FROM hallinta
WHERE username = :user AND hash = :hash");
$stmt->bindParam(':user', $myusername);
$stmt->bindParam(':hash', $hash);
$stmt->execute();
$count = $stmt->rowCount();
while($row = $stmt->fetch()) {
$r=$row["hash"];
}
Works fine with only one WHERE statement, but when both :user and :hash is in query I will get error
ERROR: 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"
What is wrong in query?
For some extremely strange reason you omit the most meaningful part of the error message, which actually were pointing out to the error.
But nevertheless, this is not PDO error but SQL error. You are supposed to test your queries in a console first, and then use in PDO only a tested one.
Also, you have to pay more attention to the queries you run. There are no syntax errors in one you posted here.

What is wrong with this SQL query? A nonsensical error is occurring.

I'm using the following SQL query:
SELECT * FROM Articles WHERE Name = 'Name' AND Column = 'Column'
And I'm receiving the following error:
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 'Column = 'Column' LIMIT 0, 30' at line 1
I see no syntax error whatsoever. What is the problem?
Column is a reserved word so you need to encapsulate it between ` chars:
AND `Column` = 'Column'
It's a bad idea to have a column named column, which is a reserved word. Can you change it in your table? Rather than having to work around the design in every query that references it, you're better off fixing it if you can.