Why this PHP PDO Mysql code not working? - php

I used Like operator and pass all the parameter But Still the following code is not working:
public function get_locations($lang, $suggest){
$this->lang = $lang;
$this->suggest = $suggest;
$sql = "SELECT l.location_id, l.location_name_col
FROM test_db.location_translations as l
WHERE l.location_name_col like LIKE :suggest
AND l.language_code = :lang
";
$params = array(':suggest'=>"%".$this->suggest."%", ':lang'=> $this->lang);
$stmt = $this->conn->prepare($sql);
$stmt->execute($params);
}
I am getting the following erros:
PDOException' with message '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 'LIKE '%A%'
AND l.language_code = 'en'' at line 3
please help me.

Well looking at your error code the problem has to do with the 'LIKE' parameter, I see that you are using 'like' and 'LIKE'. I think it should look like this:
$sql = "SELECT l.location_id, l.location_name_col FROM
test_db.location_translations as l WHERE l.location_name_col LIKE
:suggest AND l.language_code = :lang ";
What if you run it again with the above code, what happens then?

Related

WordPress database error: You have an error in your SQL syntax

Below WordPress database error is coming:
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 for query SELECT * FROM wp_author_followers WHERE
author_id = made by require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'),
Here is my code.
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}author_followers WHERE author_id = $author_id", OBJECT );
$followcounter = count($results);
return $followcounter;
Everything was correct, Just Author ID was not there, I have fixed this issue. Thanks for your attention and help.
echo "SELECT * FROM {$wpdb->prefix}author_followers WHERE author_id = $author_id";
Try to echo $author_id before the SQL query, and check SQL statement after replacing $author_id with its value.
Thanks

Mysqli query with zerofill

Hello stackoverflow community, I need help with this mysqli query. My auto increase column in database is ZERO-FILL element for example 00001 so when I try to query like this:
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '".$this->clean_k_name."',
pavard = ".$this->clean_k_surname.",
WHERE korteles_nr = '00001'");
And I get 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 'WHERE korteles_nr = 00001' at line 18
I was searching Internet for solving this problem. But can't find so please help!
Query should be like this:-
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '$this->clean_k_name',
pavard = '$this->clean_k_surname'
WHERE
korteles_nr = '00001'");
You have to remove the comma after $this->clean_k_surname variable

PDO syntax error for SELECT query

I have a basic query which gets all fields and exports the file but it keeps giving me an error. My code looks like this:
$array = ['users'];
foreach($array AS $i){
$file = $i.'.sql';
$stmt = $pdo->prepare("SELECT * FROM ? INTO OUTFILE ?");
try {
$stmt->execute(array($i,$file));
} catch (PDOException $e) {
$log .= $e -> getMessage().'........ \n ';
}
}
I keep getting this error how ever:
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 ''users' INTO OUTFILE 'users.sql'' at line 1.
What is the correct syntax for this ?
It(Syntax) should be the other way around
SELECT * INTO OUTFILE ? FROM ?;
See the documentation here
EDIT :
As JAL clarified, The table name cannot be passed as a parameter under a PreparedStatement. So your query should be like
SELECT * INTO OUTFILE ? FROM users;
You cannot prepare a statement where the table name is a parameter.
See Can PHP PDO Statements accept the table or column name as parameter?

MySql Syntax error - 42000

I'm getting this error :
Warning: PDO::query() [pdo.query]: 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 'When, RDV, Comments FROM distributions WHERE IDFond = 1' at line 1 in /Applications/XAMPP/xamppfiles/htdocs/JG/DistributionManager.class.php on line 56
When executing this code :
$Distribution_Manager->getListByFunds($Selected_Fond->id());
foreach ($Distribution_Manager as $Distrib)
{
echo $Distrib->Comments();
}
Here is the concerned function :
public function getListByFunds($FundID)
{
$Distribution = array();
$q = $this->_db->query('SELECT id, IDClient, IDFond, Who, When, RDV, Comments FROM distributions WHERE IDFond = '.$FundID);
while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
{
$Distribution[] = new Distribution($donnees);
}
return $Distribution;
}
Should be a little mistake but I'm lagging on it for almost 50 minutes !
Thanks in advance for the help ;)
WHEN is a mysql reserved word, so try using a different column name or enclose WHEN in backquotes.
When is mysql keyword, try this
SELECT id, IDClient, IDFond, Who, When AS anything..
or enclose this keyword to backquotes
SELECT id, IDClient, IDFond, Who, `When`, RDV..
Give back quotes for column names like id and try it
You're never closing your quotes on your statement.

Mysql trouble with like and or like

SELECT `id`, `name_person`, `person_content`, `datetime`
FROM (`achievers_unverified`)
WHERE ` name_person LIKE '%ved%'
OR ` person_content LIKE '%ved%' LIMIT 10
This is the sql query i am trying to use where ved is the search term.
i am gettin a 1064 error.
the codeigniter code generating it is.
$this->db->select($select)
->from($table)
->like($str[1], $query, 'both')
->or_like($str[2], $query, 'both')
->limit($offset+10, $offset);
this is the 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 'person_content LIKE '%ved%' LIMIT 10' at line 3.
$str = explode(",", $select);
where $select = id, name_person, person_content, datetime
found the solution use trim($str[0]) and trim($str[1]) the sapce was creating the problem.

Categories