SQL Syntax Error 1064 (MyBB) - syntax

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 ') ORDER BY price ASC' at line 1
Query:
SELECT * FROM mybb_newpoints_subscriptions WHERE sid IN() ORDER BY price ASC
Code snip:
$subplans = '';
if($sids != '')
{
$where = 'sid IN('.implode(',', $sids).')';
$query = $db->simple_select('newpoints_subscriptions', '*', $where, array('order_by' => 'price', 'order_dir' => 'ASC'));
while ($sub = $db->fetch_array($query))
How can I fi​​​​x th​​​is? It's been a​​​ problem for a while and ​​​​​​​​I am not very experienced with P​​HP. If you need more d​​​​eetz let me k​​​​​now. Th​​​anks! :)​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Related

How to write raw query in Laravel?

There is a query:
SELECT ST_DistanceSpheroid(geometry(location), ST_GeomFromText('POINT(37.854289 55.685333)'), 'SPHEROID["WGS 84",6378137,298.257223563]')
FROM users
How to pass parameter 37.854289 55.685333?
Also I tried this:
$point = "37.854289 55.685333";
return DB::table('users')
->select(DB::raw('ST_DistanceSpheroid(geometry(location), ST_GeomFromText(\'POINT(?)\'), \'SPHEROID["WGS 84",6378137,298.257223563]\''), [$point])
->get();
I got this error:
"message": "stripos(): Argument #1 ($haystack) must be of type string, array given",
My attempt bases accepted question:
$lon = 37.857397;
$lat = 55.685333;
return DB::table('users')
->selectRaw(
"(ST_DistanceSpheroid(
geometry(location),
ST_GeomFromText('POINT(? ?)'),
'SPHEROID[?, ?, ?]'
)) as distance",
[$lon, $lat, 'WGS 84', 6378137, 298.257223563]
)->leftJoin('doctors', 'doctors.user_id', 'users.id')->orderBy('distance', 'ASC')->get();
I have got an error:
{
"message": "PDO: SQLSTATE[XX000]: Internal error: 7 ОШИБКА: SPHEROID parser - couldnt parse the spheroid\nLINE 4: 'SPHEROID[?, ?, ?]'\n ^ (SQL: select (ST_DistanceSpheroid(\n geometry(location),\n ST_GeomFromText('POINT(37.857397 55.685333)'),\n 'SPHEROID[WGS 84, 6378137, 298.257223563]'\n )) as distance from \"users\" left join \"doctors\" on \"doctors\".\"user_id\" = \"users\".\"id\" order by \"distance\" asc)"
}
The row query that works:
SELECT doctors.user_id, (ST_DistanceSpheroid(geometry(location), ST_GeomFromText('POINT(37.857397 55.690576)'), 'SPHEROID["WGS 84",6378137,298.257223563]')
) as distance FROM users INNER JOIN doctors ON doctors.user_id = users.id ORDER BY distance ASC
You almost got it. The [$point] parameter should be the second parameter of DB::raw($query, $bindings) but you added it as a second parameter to select().
// What you have
->select(DB::raw(...), [$point])
// correct syntax
->select(DB::raw(..., [$point]))
If you've got nothing else to put in your select clause, might as well use selectRaw(). It's the same as select(DB::raw()).
DB::table('users')
->selectRaw('ST_DistanceSpheroid(geometry(location), ST_GeomFromText(\'POINT(?)\'), \'SPHEROID["WGS 84",6378137,298.257223563]\')', [$point])
Personally, I'd write the query like this:
$query = DB::table('users')
->selectRaw(
"ST_DistanceSpheroid(
geometry(location),
ST_GeomFromText('POINT(? ?)'),
'SPHEROID[?, ?, ?]'
)",
[37.854289, 55.685333, 'WGS 84', 6378137, 298.257223563]
)
->get();

This query works well in Mysql but in Postgres it is giving error

This query works fine when I was using Mysql, now that we've migrated to Postgres, it's giving an error. Where is the problem?
public function scopeClosestTo(\Illuminate\Database\Eloquent\Builder $query, array $coord = null)
{
if ($coord && isset($coord['longitude'], $coord['latitude'])) {
return $query->select([
'*',
'distance' => DB::table( 'offers as offers_temp' )->selectRaw(
'ST_Distance_Sphere(point(`longitude`, `latitude`), point(?, ?)) AS distance',
[$coord['longitude'], $coord['latitude']]
)->whereColumn('offers_temp.id', 'offers.id')
])
->withCount(['favoriteOffers'])
->where('published', '=', true)
->where('active', '=', true)
->whereNotNull('longitude')
->whereNotNull('latitude')
->whereDate('expires_at', '>', \Carbon\Carbon::now())
->orWhereNull('expires_at')
->orderBy('distance');
}
return $query;
}
Error:
"SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near ","\nLINE 1: ...ct , (select ST_Distance_Sphere(point(longitude, latitud...\n ^ (SQL: select *, (select ST_Distance_Sphere(point(longitude, latitude`), point(-43.3722344, -22.7867144)) AS distance from "offers" as "offers_temp" where "offers_temp"."id" = "offers"."id") as "distance", (select count() from "favorite_offers" where "offers"."id" = "favorite_offers"."offer_id" and "favorite_offers"."deleted_at" is null) as "favorite_offers_count" from "offers" where (("published" = 1 and "active" = 1 and "longitude" is not null and "latitude" is not null and "expires_at"::date > 2022-03-28 or "expires_at" is null) and "longitude" is not null and "latitude" is not null and exists (select * from "offer_categories" inner join "offers_offer_categories" on "offer_categories"."id" = "offers_offer_categories"."offer_category_id" where "offers"."id" = "offers_offer_categories"."offer_id" and "offers_offer_categories"."offer_category_id" in (1) and "offer_categories"."deleted_at" is null) and "to_companies" = 0 and "published" = 1 and "active" = 1 and "expires_at"::date > 2022-03-28 or "expires_at" is null) and "offers"."deleted_at" is null order by "distance" asc limit 15 offset 0)"
Your query uses backticks to escape column names, which works in MySQL. However, PostgreSQL uses double quotes to escape column names.
Change
point(`longitude`, `latitude`)
To
point("longitude", "latitude")
However, the words longitude and latitude are not reserved words in postgres, so there should be no reason you need to quote them.
See this article on the PostgreSQL wiki for more about moving from MySQL to PostgreSQL.

How do I fix this SQL Syntax error concerning blobs?

i have a new forum up using SMF, and i’m trying to convert the database to UTF-8 now. SMF provides an easy way to do this, but upon doing so, i get this strange sounding error:
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 'before before blob not null,
change column after after blob not null,
...' at line 6
file: /home/halfmoon/public_html/sources/managemaintenance.php
line: 664
i’m an extreme newbie to coding, especially when it comes to SQL, so i have 0 idea what the error means, other than where to find it and that the line is incorrect. if need be i can also copy and paste the line’s full text here as well- not sure what all is needed!:
line 664 starts at ‘updates_blob’
SQL Vers.:
“
// Change the columns to binary form.
$smcFunc['db_query']('', '
ALTER TABLE {raw:table_name}{raw:updates_blob}',
array(
'table_name' => $table_info['Name'],
'updates_blob' => substr($updates_blob, 0, -1),
)
);
MariaDB Vers.:
// Convert the character set if MySQL has no native support for it.
if (isset($translation_tables[$_POST['src_charset']]))
{
$update = '';
foreach ($table_charsets as $charset => $columns)
foreach ($columns as $column)
$update .= '
' . $column['Field'] . ' = ' . strtr($replace, array('%field%' => $column['Field'])) . ',';
$smcFunc['db_query']('', '
UPDATE {raw:table_name}
SET {raw:updates}',
array(
'table_name' => $table_info['Name'],
'updates' => substr($update, 0, -1),
)
);
}port for it.”

Sum in laravel db query

I need to get the Sum of ProductQTY groupBy ProductID while using join, I always get an error when using db::raw, attached here is my code
$pick_list_items = DB::table('pick_list_detail')
->where('pick_list_detail.pick_list_id',$id)
->join('sale_invoices', 'pick_list_detail.sale_invoice_id','=','sale_invoices.id')
->join('sale_invoice_detail', 'sale_invoice_detail.sale_invoice_id','=','pick_list_detail.sale_invoice_id')
->select(['pick_list_detail.sale_invoice_id', 'sale_invoice_detail.product_id', 'sale_invoice_detail.product_qty', 'sale_invoice_detail.uom', 'sale_invoice_detail.uom_factor'])
->sum('sale_invoice_detail.product_qty')
->groupBy('sale_invoice_detail.product_id')
->get();
I'm using laravel 5.4
Here is the error
(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1055 'fdis.pick_list_detail.sale_invoice_id' isn't in GROUP BY (SQL: select
pick_list_detail.sale_invoice_id,
sale_invoice_detail.product_id,
sale_invoice_detail.product_qty, sale_invoice_detail.uom,
sale_invoice_detail.uom_factor from pick_list_detail inner join
sale_invoices on pick_list_detail.sale_invoice_id =
sale_invoices.id inner join sale_invoice_detail on
sale_invoice_detail.sale_invoice_id =
pick_list_detail.sale_invoice_id where
pick_list_detail.pick_list_id = 1 group by
sale_invoice_detail.product_id)
$sale_invoices = DB::table('pick_list_detail')
->select(DB::raw('sum(sale_invoice_detail.product_qty) as si_count, pick_list_detail.pick_list_id , sale_invoice_detail.product_id , sale_invoice_detail.uom, sale_invoice_detail.uom_factor '))
->where('pick_list_detail.pick_list_id',$id)
->join('sale_invoices', 'pick_list_detail.sale_invoice_id','=','sale_invoices.id')
->join('sale_invoice_detail', 'sale_invoice_detail.sale_invoice_id','=','pick_list_detail.sale_invoice_id')
->groupBy('pick_list_detail.pick_list_id')
->groupBy('sale_invoice_detail.product_id')
->groupBy('sale_invoice_detail.uom')
->groupBy('sale_invoice_detail.uom_factor')
->get();
Raw Query is my solution.

Query Builder reading conditional like a column - Unknown column in field list

I have a MySQL expression that uses a conditional IF statement. As a MySQL expression, it returns TRUE or FALSE, but when I use it in CodeIgniter's query builder, I get an error. The error suggests that the outcome of the conditional is reading like a column, but how can I fix this? Thanks.
MySQL:
SELECT
positions.max_vol AS attendee_limit,
COUNT(users_positions.user_id) AS total_attendees,
IF(COUNT(users_positions.user_id) < positions.max_vol
OR positions.max_vol IS NULL,
TRUE,
FALSE) AS result
FROM
positions
INNER JOIN
users_positions ON positions.id = users_positions.position_id
WHERE
positions.id = 16
AND users_positions.calendar_date = '2016-09-05'
Function:
private function check_attendee_limit($pos_id = NULL, $date = NULL)
{
$this->db->select('positions.max_vol, COUNT(users_positions.user_id), IF(COUNT(users_positions.user_id) < positions.max_vol OR positions.max_vol IS NULL, TRUE, FALSE)');
$this->db->from('positions');
$this->db->join('users_positions', "positions.id = users_positions.position_id", 'inner');
$this->db->where('positions.id', $pos_id);
$this->db->where('users_positions.calendar_date', $date);
$query = $this->db->get();
return $query->result(); // return the rows selected
}
Error:
A Database Error Occurred
Error Number: 1054
Unknown column 'TRUE' in 'field list'
SELECT `positions`.`max_vol`, COUNT(users_positions.user_id), IF(COUNT(users_positions.user_id) < positions.max_vol OR positions.max_vol IS NULL, `TRUE`, FALSE)
FROM `positions`
INNER JOIN `users_positions` ON `positions`.`id` = `users_positions`.`position_id`
WHERE `positions`.`id` = '15'
AND `users_positions`.`calendar_date` = '2016-09-05'
Filename: models/projects/Calendar_model.php
Line Number: 141
CI adds backticks - which means you've to prevent CI from escaping your select
Try this instead
$this->db->select('positions.max_vol, COUNT(users_positions.user_id), IF(COUNT(users_positions.user_id) < positions.max_vol OR positions.max_vol IS NULL, TRUE, FALSE)', false);
If you are worry about security - in your case it doesn't matter because you don't use any user input in your select query.
You can find more informations about the Query Builder here

Resources