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();
}
Related
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?
By executing below query i getting this error message
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 'status=1' at line 1 SELECT username,useremail FROM tbl_cart where user_id= 8AND status=1
query
$query = $this->db->query('SELECT username,useremail FROM tbl_cart where
user_id= '.$this->session->userdata('userId').'AND status=1' );
$resultdata['results'] = $query->result_array();
You need to insert an space before AND
$query = $this->db->query('SELECT username,useremail FROM tbl_cart where
user_id= '.$this->session->userdata('userId').' AND status=1' );
^ here
The better way to do it with CI is
$query = $this->db->select('username, useremail')
->where('user_id', $this->session->userdata('userID'))
->where('status',1)
//instead of from CI does get
->get('tbl_cart')->result_array();
result_array() returns the results as an array of arrays while result returns query as an array of objects
if you are using CI it is better to fully take advantage of the query builder classes
I am trying to count the amount of notifications a user has got in Codeigniter. I am comparing the timestamp that is saved in each users row that records when they last checked the notifications page and the time added for each notification. However I am getting a database error:
A Database Error Occurred
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 '10:17:00' at line 5
SELECT * FROM (`notifications`) WHERE `user_id` = '1' AND `instigator_id` != '1' AND added <= 2014-11-04 10:17:00
Filename: /Users/true/Documents/Site/models/private/notifications_model.php
Line Number: 152
The Notifications controller looks like this
$data["user"] = $CI->Profile_model->get_public_profile_id($logged_user);
$count = $CI->notifications_model->get_notifications_count($data["user"][0]["notes_check"]);
The model:
public function get_notifications_count($time){
$this->db->from($this->notifications_table);
$this->db->where('user_id', $this->session->userdata("id", 0));
$this->db->where('instigator_id !=', $this->session->userdata("id", 0));
$this->db->where('added <= '.$time, '', false);
$query = $this->db->get();
$results = $query->result_array();
return count($results);
}
Your date/time values are strings. Since you don't have '-quotes around them, MySQL is seeing this:
added <= 2014-11-04 10:17:00
^^^^^^^^^^---mathematical subtraction = 1999
^^^^^^^^^--illegal unknown field/table name
It should be
added <= '2014-11-04 10:17:00'
Note the quotes.
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?
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