Syntax Error or access violation - SQL query & Laravel - mysql

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

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.

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 + PHP In MySQL IF statement is wrong? How to create simple If statement MySQL 5.50?

I use this code but I see syntax error.
MYSQL 5.5.50.0
IF 2>1 THEN
SELECT "HELLO WORLD!"
END IF;
EDIT: This code Will not work. Because IF Statement only using FUNCTION and procedure.
Error is:
SQL-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 'IF 2>1 THEN
my readSql($sql) function is working most case. I think my MySQL code is wrong.
What is wrong?
Thank you.
EDIT:
IF Statement only using FUNCTION and procedure.
MySQL IF() takes three expressions and if the first expression is true, not zero and not NULL, it returns the second expression. Otherwise, it returns the third expression.
Depending on the context in which it is used, it returns either numeric or string value.
IF(expression ,expr_true, expr_false);
For example, you could have a query:
SELECT IF(1>3,'true','false');
#return false
If you just want return the string 'HELLO WORLD' , you could use:
SELECT CASE WHEN 2 > 1 THEN 'HELLO WORLD' END

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

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.