Laravel Concat - Syntax or Access Violation - php

Trying to write an MySQL Query to search on users based on two columns :
first_name
surname
I'd like to Concatenate first_name and surname as name and then write a search query based on that.
I've currently written some PHP code, But I keep getting the error :
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 '%?%' at line
1 (SQL: select users.* from users where CONCAT(first_name, ' ',
surname) LIKE %Stu%)
My current PHP code is as follows :
// Search Based on Q..
$q = $request->input('q');
$results = User::select('users.*')->WhereRaw("CONCAT(`first_name`, ' ', `surname`) LIKE %?%", [$q])->get();
I suspect I'm just getting the syntax wrong, But any help would be appreciated.
Thank You!

Move % to parameter:
$results = User::select('users.*')
->WhereRaw("CONCAT(`first_name`, ' ', `surname`) LIKE ?", ['%' . $q . '%'])
->get();

two errors in your query first as fullname not mention and second like operator.. i have fix these errors
$q = $request->input('q');
$results = User::select('users.*')
->WhereRaw("CONCAT(`first_name`, ' ', `surname`) as fullname
LIKE ?", ['%' . $q . '%'])->get();

Related

Case insensitive username checking? [duplicate]

I'm trying to select some data from a mysql table but I cannot get the Where comparison to be case insensitive, I tried using LOWER:
$wildcard = $_GET['q'];
$query = "SELECT id, name, departamento FROM gestionDoc_cargos WHERE (LOWER(name) LIKE '%' LOWER(:wildcard) '%' OR LOWER(departamento) LIKE '%' LOWER(:wildcard) '%')";
try{
$result = DB::getInstance()->prepare($query);
$result->bindParam(':wildcard', $wildcard, PDO::PARAM_STR);
$result->execute();
$result = $result->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
die($e->getMessage());
}
print_r($result);
echo json_encode(array_values($result));
but 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 'LOWER('C') '%' OR LOWER(departamento) LIKE '%' LOWER('C') '%')' at line 1
and if I remove the LOWER from the query I get a case sensitive select.
This
...snip... ) LIKE '%' LOWER(:wildcard) '%' OR ...snip
is incorrect. You've got a string ('%') followed by a function call (LOWER()) followed by another string, and they're just sitting there - no connecting logic, no concatenation, blah blah blah .
It should be
... LIKE CONCAT('%', LOWER(:wildcard), '%') OR ...
And by default, mysql comparisons ARE case insensitive, unless you force a binary comparison, or you're using a case sensitive collation on your db/table.

PHP syntax error in SELECT mySQL query

I am trying to execute this mySQL query in PHP.
$sql = "SELECT * FROM Property
WHERE CONCAT(name, '',
contact_number , '',
hostel_address,'',
renter_name,'',
other_details,'',
date_posted,'') LIKE '%".$var."'
ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y')";
An I am getting following 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 'ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y') ASC' at line 1
Any help here :(
may try this:
$sql = "SELECT * FROM Property WHERE CONCAT(name, '',contact_number , '', hostel_address,'',renter_name,'',other_details,'',date_posted,'') LIKE '%".$var."' ORDER BY STR_TO_DATE(date_posted,'%d/%m/%Y')";
Note that there should be an ending single quote before ORDER BY

Error in SQL Syntax while trying to convert database

I got an Error while trying to convert my database.
Fehler in 163 - 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 2
Query war : SELECT * FROM phpbb_users WHERE user_active = 1 AND user_id NOT IN ()
My Code:
$e0 = query("SELECT * FROM ".$phpbb_prefix."_users
WHERE user_active = 1 AND user_id NOT IN (".implode("','", $ilch_to_phpbb).")" , $phpbb_con)
or die('Fehler in '. __LINE__ . ' - '.mysql_errno($phpbb_con) . ' : '. mysql_error($phpbb_con).'<br /> Query war : ' . $lastquery . '<hr />');
I can not recognize the error. Any Ideas?
Database: MySQL(i) 5.5.53-0+deb7u1
PHP: 5
The problem is with your implode. Imagine if $ilch_to_phpbb contains 2 user id's, the query will be:
SELECT * FROM ".$phpbb_prefix."_users
WHERE user_active = 1 AND user_id NOT IN (2','3)
That's obviously invalid SQL syntax.
You either need to drop the ' in the implode (although this will cause a SQL error if $ilch_to_phpbb is empty, because NOT IN () is invalid SQL), or add ' around it in the query:
AND user_id NOT IN ('".implode("','", $ilch_to_phpbb)."')

I cannot figure out why I am getting this MySQL syntax error (PHP PDO) (MySQL)

I am trying to do a simple insert into my MySQL database, but I get this syntax 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 MySQL server version for the right syntax to use near
'Combined Authority - ICT Service Desk Technician (WYCA 53) ' at line 2
Why?
Query:
$conn->exec("INSERT INTO jobs (jobname, category, contract, link)
SELECT * FROM (" . $name[$i] . "," . $category[$i] . "," . $contract[$i]
. "," . $link[$i] . ") AS tmp
WHERE NOT EXISTS (
SELECT link FROM jobs WHERE link = '" . " " . $link[$i] . ") LIMIT 1;");
$sql printed:
INSERT INTO jobs (jobname, category, contract, link) SELECT * FROM ( West
Yorkshire Combined Authority - ICT Service Desk Technician (WYCA 53)
Details ,' Other ',' Other
','https://bradford.engageats.co.uk/ViewVacancyV2.aspx?
enc=mEgrBL4XQK0+ld8aNkwYmF3VpPuSfX9mpz94c96U/BBgu1IZbwnQ0d+smFL6YrlPhdWkSGi559WmVou+xCXKsYHbHKP0EyHRCwf+vYTu8aYRJbtJgz78Wm2KQgu+LktushGT2Rg0PHjiRMA2Xyn4gw==') AS tmp WHERE NOT EXISTS ( SELECT link FROM jobs WHERE link ='https://bradford.engageats.co.uk/ViewVacancyV2.aspx?enc=mEgrBL4XQK0+ld8aNkwYmF3VpPuSfX9mpz94c96U/BBgu1IZbwnQ0d+smFL6YrlPhdWkSGi559WmVou+xCXKsYHbHKP0EyHRCwf+vYTu8aYRJbtJgz78Wm2KQgu+LktushGT2Rg0PHjiRMA2Xyn4gw==') LIMIT 1;
Apologies for the poor formatting above. Please copy and paste it into a text editor to view it better.
EDIT:
Strangely, this query works with dummy values, but it's still not working for arrays
INSERT INTO jobs (jobname, category, contract, link)
SELECT * FROM (SELECT 'Test', 'Test2',
'Test3','https://bradford.engageats.co.uk/ViewVacancyV2.aspx?
enc=mEgrBL4XQK0+ld8aNkwYmEUlxXraCLcDtY5P6rS92ks+pMDnlWa9QO6M/Df/HLticzgbgVWV
YayJj+zNDXalJnejkDY/4/gH0pIF9KyvMFXjn0u0quGSUzf4M/Gh0wF0MqIRgwLERFf+xXj6lw4s
tQ==') AS tmp
WHERE NOT EXISTS (
SELECT link FROM jobs WHERE link = 'https://bradford.engageats.co.uk/ViewVacancyV2.aspx?enc=mEgrBL4XQK0+ld8aNkwYmEUlxXraCLcDtY5P6rS92ks+pMDnlWa9QO6M/Df/HLticzgbgVWVYayJj+zNDXalJnejkDY/4/gH0pIF9KyvMFXjn0u0quGSUzf4M/Gh0wF0MqIRgwLERFf+xXj6lw4stQ=='
) LIMIT 1;
try this select query and do same with other variables :
SELECT * FROM ("'".$name[$i]."','".$category[$i]."', '".$contract[$i] ."', '". $link[$i] ."'") AS tmp
I can spot three different problems:
First of all, you are inventing your own SQL syntax and the server is not amused. You cannot SELECT * FROM (anything you want). You can only select from tables, views or subqueries.
Secondly, when you type e.g. foo in SQL the database engine needs a way to figure out if you mean a table or column or you mean literal word. The method used is single quotes:
SELECT foo AS this_is_a_column, 'foo' AS this_is_a_value
FROM bar
You can find more details at What is the difference between an identifier and a literal?
Last but not least, your overall use of the PDO extension is wrong. PDO provides a way to separate code and data but you are not using it. Rather than this:
$conn->exec("SELECT link FROM jobs WHERE link = '" . " " . $link[$i] . ") LIMIT 1;");
... you should be doing something like this:
$stmt = $conn->prepared("SELECT link FROM jobs WHERE link=? LIMIT 1");
$stmt->execute($stmt, array($link[$i]));
Use Quotes for string litterals
backticks for columns and tables names.
For more reference Check: http://php.net/manual/en/pdo.errorinfo.php

Error when preparing query - MySQLIi class "SHOW TABLES LIKE" error

I am using this database class for my project: GitHub.
When trying to execute a SHOW query to determine whether a table exists or not I receive this error:
Fatal error: Problem preparing query (SHOW TABLES LIKE users) 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 'users' at line 1 in mysqli.php on line 679
The query looks like this:
$result = $DATABASE->rawQuery("SHOW TABLES LIKE " . $TABLE);
$TABLE is obviously filled with a string, I double checked that.
Any idea what could be wrong?
You probably missed the quotes:
$result = $DATABASE->rawQuery("SHOW TABLES LIKE '" . $TABLE . "'");
The like statement it's value is wrong.
You should use:
BAD
$result = $DATABASE->rawQuery("SHOW TABLES LIKE 'value here' ");
Good
$result = $DATABASE->rawQuery("SHOW TABLES LIKE ? ");
$DATABASE->addParam($table);
I think you allso want to add % in front and after your $table :)

Categories