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
Related
I want to get a time difference from data using the TIMESTAMPDIFF function, but this time i want to use a pure query builder in codeigniter
$this->db->select("TIMESTAMPDIFF(DAY, (".$this->db->select('payment_date')."), (".$this->db->select('download_date').")))",FALSE);
$query = $this->db->get('transaksi');
return $query;
I've tried the code above, but it shows an error like this :
Severity: 4096 Message: Object of class CI_DB_mysqli_driver could not
be converted to string
and like this :
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 '), ())) FROM transaksi' at line 1
SELECT payment_date, download_date, TIMESTAMPDIFF(DAY, (), ())) FROM
transaksi
is there any solution to get the data?
Solution:
$this->db->select("payment_date, download_date, TIMESTAMPDIFF(DAY, payment_date, download_date)",FALSE);
$query = $this->db->get('transaksi');
return $query->result();
Sub Query not necessary there.
$this->db->select("payment_date, download_date, TIMESTAMPDIFF(DAY, payment_date, download_date)",FALSE);
$query = $this->db->get('transaksi');
return $query->result();
I am facing very weird condition right now. I wrote a query in CodeIgniter with a WHERE condition like this:
$queryps = $this->db->query("SELECT count(workorderno) as total from crm_workorder where workorderno =".$sitecode."");
But I am getting 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 '' at line 1
SELECT count(workorderno) as total from crm_workorder where
workorderno =
Now the weird thing is this the variable $sitecode is not blank. When I echo the query, it shows this:
SELECT count(workorderno) as total from crm_workorder where workorderno =2
But in SQL query, I am getting above error. There is nothing in WHERE condition.
I tried every possible way to find out the reason behind it but I am not able to figure out this. Thanks.
This is what you need, this must be in your model.
<?php
$this->db->select("SELECT count(workorderno) as total");
$this->db->from("crm_workorder");
$this->db->where("workorderno",$sitecode);
$queryps = $this->db->get();
?>
try this
$this->db->select('count(workorderno) as total');
$this->db->from("crm_workorder");
$this->db->where("workorderno",$sitecode);
$query = $this->db->get();
if ( $query->num_rows() > 0 )
{
$row = $query->row_array();
print_r($row);
}
$queryps = $this->db->query("SELECT COUNT(workorderno) AS total FROM crm_workorder WHERE workorderno=$sitecode");
And make sure that $sitecode has a value.
TESTED
ok try this code. your error will be solve.
$queryps = $this->db->query("SELECT count(workorderno) as total from crm_workorder where workorderno ='$sitecode'");
I'm not sure if this is a codeigniter thing or specifically a mysqli thing.
In my model class I'm trying to delete using 2 WHERE arguments:
$query = "DELETE FROM users WHERE user_id = ? AND company_id = ?";
$result = $this->db->query( $query, $user_id, '1' );
Error message:
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 '? AND company_id = ?' at line 1
Any ideas?
I recommend you to use 'active record'
$this->db->where('user_id', $user_id);
$this->db->where('company_id', 1);
$this->db->delete('users');
https://ellislab.com/codeigniter/user-guide/database/active_record.html
using active record is ok but using native SQL takes less resources , I quote:
Note: If you intend to write your own queries you can disable active record
in your database config file, allowing the core database library
and adapter to utilize fewer resources.
source : https://ellislab.com/codeigniter/user-guide/database/active_record.html
here is my two lines
$query_string = "DELETE FROM users WHERE user_id =".$user_id." AND company_id = 1";
$query= $this->db->query($query_string); // will return True if SUCCESS
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
Im using dbal on Symfony2 to retrieve some info from my table:
$social = $conn->fetchAll('SELECT * FROM page WHERE brand_id = :brand LIMIT :start,:limit', array('brand'=>$brand, 'start'=>(int) $start, 'limit'=>(int) $limit));
Im getting an error only when I add the last part (LIMIT ....), this make me think that i cant limit the result inside the sql query but outside using some kind of command. How can i solve this?
Error:
An exception occurred while executing 'SELECT * FROM page WHERE brand_id = :brand LIMIT :start,:limit' with params {"brand":1,"start":0,"limit":4}:
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 ''0','4'' at line 1
$statement = $connection->prepare(
'SELECT ... LIMIT :limit'
);
$statement->bindValue('limit', $limit, \PDO::PARAM_INT);
$statement->execute();
$result = $statement->fetchAll();
Or you can simply use 3rd argument in the fetchAll($sql, array $params = array(), $types = array()) like that:
$social = $conn->fetchAll('SELECT * FROM page WHERE brand_id = :brand LIMIT :start,:limit', array('brand'=>$brand, 'start'=>(int) $start, 'limit'=>(int) $limit), array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT));