PDO basic select query - mysql

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.

Related

Syntax error or access violation on MySQL geometry data type

I am working with datatype geometry on MySQL. I try to query from the page. I got the error.
How to fix? Could you please help?
Error here:
"An exception occurred while executing '
SELECT t.`road_id`, t.`road_ref_num`, t.`chainage`, t.`road_name`,
ST_AsGeoJSON(ST_Transform(t.`road_wkt`, ?::int)) as geom
FROM gis_ruralroads_t1 t
WHERE (t.road_ref_num LIKE ?)
AND (ST_Transform(ST_SetSRID(ST_MakeBox2D(ST_Point(?, ?),
ST_Point(?, ?)), ?), ST_Srid(t.`road_wkt`)) && t.`road_wkt`
)
with params [32648,
"%0806T1001%", 5579.905, 1088581.723, 1029359.857, 1650396.77,
32648]
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 '::int)) as geom FROM gis_ruralroads_t1 t WHERE (t.road_ref_num LIKE '%0806T1001%' at line 1"
Best,
Loy
It is pointing right at ::int. MySQL does not have any syntax like that. Simply remove those 5 characters.
In general, you can trust that an integer will be correctly substituted, even if quotes are added:
ST_Transform(t.`road_wkt`, ?)
turns into this after substitution:
ST_Transform(t.`road_wkt`, '32648')
The apostrophes won't hurt.

Syntax Error or access violation - SQL query & Laravel

In my query, i am trying to compare today's date only (without time) to my field created_at but i get the error below
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 '2018-08-29 DATE(created_at)' at line 1
Controller
$item_shipped = Item::where('id',Auth::user()->id)->whereRaw('DATE(created_at)' , '=', Carbon::now()->toDateString())->get();
return $item_shipped;
How do i solve this problem ?
whereRaw doesn't accept the operator parameter. You have to write the whole condition and it takes the second parameter which is an array of bindings.
You need this:
->whereRaw('DATE(created_at) = ?', [Carbon::now()->toDateString()])->get();

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

MYSQL where clause causes syntax error

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);

Syntax error 256 in WHERE clause

I am trying to update a field in my table and I keep getting this syntax error.
global $conn, $strTableName;
db_exec("UPDATE equipment SET EContractNum = " . $_SESSION[$strTableName."_masterkey1"] . " WHERE EContractNum = " . $values['EContractNum'], $conn);
Here is the error:
Error type: 256 Error Description: 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 'WHERE econtractnum=35867111' at line
1
I have looked at several searchs that are similair to mine but I cannot figure out what I am doing wrong. I am fairly new at this so it is probably something simple. I just cant seem to make it work. Thanks for any help.
$_SESSION[$strTableName."_masterkey1"] is probably empty, or a string that needs to be quoted.
Also, don't put the raw values of variables into queries like that. Use a framework or prepared statements. It's good for security and it would also prevent this kind of error (well, it'd turn it into a different kind of error, at least).