PDO syntax error for SELECT query - php

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?

Related

Why this PHP PDO Mysql code not working?

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?

trouble with selecting from database with codeigniter

I am trying to select info from my database with codeigniter. Here is the code:
public function getContent($table_name){
$show = false;
$sql = 'SELECT * FROM ?';
$query = $this->db->query($sql, array($table_name));
if($query->num_rows > 0){
$show = $query->result_array();
}
return $show;
}
I get an error from mysql:
Error Number: 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 ''main_db'' at line 1
SELECT * FROM 'main_db'
Filename: C:\xampp\htdocs\Staz1\system\database\DB_driver.php
Line Number: 331
I think that the problem is that the query has 'main_db' with apostrophes. When writing the query directly into the sql section in phpmyadmin that was the problem that I encountered (it only works if written with no apostrophes or with backticks).
If this is indeed the issue - how do I send the query without the apostrophes?
public function getContent($table_name){
return $this->db->from($table_name)->get()->result_array();
}

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

MySQL Error 1064 Using LIMIT Clause

I'm having a strange issue running a query via PDO prepared statements that I just can't spot the issue. When running the query manually it works just fine. Here is the code (simplified for conciseness):
// Query Parameters
$params = array( 1, 5 );
// Get Products
$query = "SELECT * FROM mydb.Product
WHERE ProductId >= ?
AND IsApproved = 1
AND IsPublic = 1
LIMIT ?";
// Get Database Instance
$dbh = App\App::getDatabase()->getInstance();
// Prepare Query
if( $stmt = $dbh->prepare($query) )
{
if( $stmt->execute($params) )
{
// Return Records
}
}
Removing the LIMIT ? portion from the query returns all results as expected. Instead, when attempting to use the LIMIT and it passes 5 as the value, I get this 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 ''5'' at line 5
A dump of the PDOStatement object after preparation shows:
object(PDOStatement)[59]
public 'queryString' => string 'SELECT * FROM mydb.Product
WHERE ProductId >= ?
AND IsApproved = 1
AND IsPublic = 1
LIMIT ?'
I've tried putting a semicolon at the end of the query but that gives same error. Am I having cerebral flatulence and missing something obvious?
TL;DR; Why does my prepared statement fail when using the LIMIT clause with a 1064 error?
I think this could be a duplication of
PDO Mysql Syntax error 1064
The solution is to bind limit parameter forcing it to be an int instead of a string with a simple (int) cast.
just put (int) before your limit value

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.

Categories