CodeIgniter query syntax error - debugging

I am facing a syntax issue with a CodeIgniter database query. Can't figure out what's wrong.
$query = $this->db->query("
INSERT IGNORE INTO ".$table." (email, lang, ip_address)
VALUES (".$this->db->escape($_POST['email']).", ".$this->db->escape($lang).", ".$this->input->ip_address().")");
I am also looking for a way to output what the query looks like once the placeholders are replaced, as I am little confused with CodeIgniter debugging options.

It looks as though you are not escaping the strings that you're trying to input into the database. The query you've posted would evaluate to something like:
$query = $this->db->query("
INSERT IGNORE INTO table_name (email, lang, ip_address)
VALUES (email#email.com, en, 192.168.0.1)
");
This will throw an error as the strings in VALUES are not properly escaped. Instead of the query you're running you should use something like:
$query = $this->db->query("
INSERT IGNORE INTO ".$table." (email, lang, ip_address)
VALUES ('".$this->db->escape($_POST['email'])."', '".$this->db->escape($lang)."', '".$this->input->ip_address()."')
");
Note the new ' characters around each string.

use
echo $this->db->last_query();
for retrieving the query runned.
so then check if the query is well formatted.

To know what query you are passing to your database. Use below statement and to insert data into the database. Please follow the below procedure.
echo $this->db->last_query();
$data = array(
'email' => $this->db->escape($_POST['email']),
'lang' = > $this->db->escape($lang),
'ip_address' => $this->input->ip_address(),
);
Call your model function $this->model->insert_function_name($data);
Your model function in your model file
public function insert_function_name($data)
{
$this->db->insert($table_name,$data);
return $this->db->insert_id();
}

Try this : your query was missing single quotes to the string type of value like email, lang and ip
$query = $this->db->query("
INSERT IGNORE INTO ".$table." (email, lang, ip_address)
VALUES ('".$this->db->escape($_POST['email'])."', '".$this->db->escape($lang)."', '".$this->input->ip_address()."')");

Related

Codeigniter Query binding multiple fields with the same value

I'm in a situation where I'm doing a MySQL query with Codeigniter and where I have a lot of fields value request which are ALL the same.
Example:
$this->db->query('SELECT * FROM abc WHERE user_id = ? AND msg_from = ? AND msg_to != ?', [$id, $id, $id]);
This has just 3 question marks but the query I'm working on is HUGE and has 19 question marks WHICH ARE ALL THE SAME variable.
So I was trying to figure out how to tell Codeigniter all question marks are pointing to the same variable without having to fill an array with 19 times the same variable.
I thought of a for-loop but I wanted to know if a shortcut exist.
you should be able to do this with Codeigniters Query Builder pretty easily
Something like that should work:
$this->db
->select('*')
->from('abc');
$arrFields = array('users_id', 'msg_from', 'msg_to');
foreach($arrFields AS $val)
{
$this->db->where($val, $id);
}
$query = $this->db->get();

What is the equivalent query of laravel on this?

This is the native sql:
$sql = "Select count(name) from users Where email = 't#t.com' and user_id = 10";
I have this laravel code:
$checker = Customer::whereEmailAndUserId("t#t.com",10)->count("name");
Is this a correct way to do it in laravel?
You have to use where helper function and pass an array of checks. For example in your code it will be:
$checker = Customer::where([
['email', '=', 't#t.com'],
['user_id' '=', '10']
])->count();
Note: Please use the appropriate column name as it in table.
Assuming Customer model represents table users, you'll get query with eloquent like this:
Customer::where('email', 't#t.com')->where('user_id', 10)->select(\DB::raw('count(name)'))->get();
The option you are trying is incorrect
here is the right option
$users = \App\Customer::where('email','t#t.com')
->where('user_id',10)
->count()
Explanation of above code
App\Customer is the Model class and I am trying to read records where email = 't#t.com you can use various comparison operators like <,> and so on and you can also use the same function to for string pattern matching also
Eg.
$users = \App\Customer::where('email','%t.com')
->where('user_id',10)
->count()
You can use the same where function for Null Value test also
Eg.
$users = \App\Customer::where('email','=', null)
->where('user_id',10)
->count()
The above where clause will be converted to is null test of the SQL
You can read more here

Joomla database query SELECT AS

So I'm just hypothetically thrilled to be querying my hypothetical database:
$query->select($db->quoteName(array('user_id', 'name')));
I would, however, like the query to look like:
SELECT `user_id` AS `uid`, `name` AS `User-Name`
How the heck do I get the AS in there?
I know this question is 6 months old, so you've probably found an answer or worked around it, but for anyone else who has a similar problem:
$query->select($db->quoteName(array('user_id','name'),array('uid','User-Name')));
If you only want to use an alias for some fields, just pass null in the array for the fields you don't want to alias, so for example:
$query->select($db->quoteName(array('user_id','name'),array(null,'User-Name')));
would give
"SELECT `user_id`, `name` AS `User-Name`"
My preferred way is this:
I create an array with the fields I want to select:
$fields = array(
'a.id' => 'id',
'a.field1' => 'field1',
'a.field2' => 'field2',
'a.field3' => 'field3',
'b.field1' => 'bfield1',
'b.field2' => null,
'b.field3' => 'bfield3',
);
In the above array, the keys are used for the db Column names of the query, the values for the aliases, as you can see later in the $query->select().
*When you do not need an alias - just set null.
This helps better to control and check what fields I want and how to name them - and is better for maintenance or changes and is portable enough, as I only have to change my $fields array according to my needs.
Then the Joomla select command can be like:
$query->select( $db->quoteName(
array_keys($fields),
array_values($fields)
));
This will produce the following SQL SELECT query:
SELECT `a`.`id` AS `id`,`a`.`field1` AS `field1`,`a`.`field2` AS
`field2`,`a`.`field3` AS `field3`,`b`.`field1` AS
`bfield1`, `field2`, `b`.`field3` AS `bfield3`
Try the following:
$query->select($db->quoteName('user_id') .' AS '. $db->quoteName('name') .' AS '. $db->quoteName('User-Name'));
Be sure to use the full query as described here:
http://docs.joomla.org/Selecting_data_using_JDatabase
So yours will look something like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('user_id') .' AS '. $db->quoteName('name') .' AS '. $db->quoteName('User-Name'));
$query->from($db->quoteName('#__tablename'));
$db->setQuery($query);
$results = $db->loadObjectList();
Please note that I havent tested this query using AS so let me know if it works or not
I've got this to work:
->select($db->quoteName(array('user_id`' . ' AS ' . '`uid', 'name`' . ' AS ' . '`User-Name')))
It's better to use what Joomla! suggest us.
Note by putting 'a' as a second parameter will generate #__content AS a
So, you may have something like this:
$query
->select('*')
->from($db->quoteName('#__content', 'a'))

codeigniter insert from query how to optimized

I want to optimized my query on codeigniter.
this query works but it seems, its take time to insert the result to my database table.
$this->db->select('client_id');
$this->db->from('event');
$query = $this->db->get();
foreach($query->result_array() as $row){
$client_id = $row['client_id'];
$data = array( 'event_id' => $event_id , 'client_id' => $client_id);
$this->db->insert('event_entry', $data);
}
I would like to know if theres a way it to optimized.
Instead of doing n number of inserts, doing just a single insert should improve execution time. You can achieve this in codeigniters active record by using insert_batch() .
$this->db->select('client_id');
$this->db->from('event');
$query = $this->db->get();
$data = array();
foreach($query->result_array() as $row){
$client_id = $row['client_id'];
array_push($data, array('event_id' => $event_id , 'client_id' => $client_id));
}
$this->db->insert_batch('event_entry', $data);
Produces:
INSERT INTO event_entry (event_id, client_id) VALUES ('event_id', 'client_id'), ('event_id', 'client_id'), ...
Replace all of that code with:
$this->db->query("
INSERT INTO event_entry (event_id, client_id)
SELECT ?, client_id
FROM event
", array($event_id));
And you will clearly notice the difference in execution time :) Plus in my opinion, having less code to worry about.
This query can't be run from Active Records, but it should be quite self explaining. Just like a normal SELECT, if fetches client_id and the already defined value $event_id by each event row. It then takes these values, and INSERT them into event_entry.
Note that ? and array($event_id) insert the value into the query escaped (and safe). Never insert into query as SELECT {$event_id}, client_id unless you know what you're doing.
Jeemusu's solution is indeed a nice way to do it through Active Records, but if it's performance you want all the way, one query is faster than two in this case.
you can use insert_batch command to insert data to database . Produce your data array with foreach loop and then use insert_batch database.http://ellislab.com/codeigniter/user-guide/database/active_record.html
please let me know if you need any help

Concat in Codeigniter Active Record

I am trying a Concat for an autocomplete, Using CI's Active Record.
My Query is :
$this->db->select("CONCAT(user_firstname, '.', user_surname) AS name", FALSE);
$this->db->select('user_id, user_telephone, user_email');
$this->db->from('users');
$this->db->where('name', $term);
I keep getting an MySQL Error from this saying:
Error Number: 1054Unknown column 'name' in 'where clause'
Which is true, However I have just created in my Concat clause. I ideally need $term to match the Concatenated firstname and surname fields.
Any ideas what I can do to improve this? I am considering just writing this as an flat MySQL Query..
Thanks in advance
$this->db->select('user_id, user_telephone, user_email, CONCAT(user_firstname, '.', user_surname) AS name', FALSE);
$this->db->from('users');
$this->db->where('name', $term);
Not sure why you are running multiple selects. So just put it as a single select. It's probably that the 2nd one is overriding the first one and thus overwriting the concatenation to create the name column.
$this->db->select("CONCAT((first_name),(' '),(middle_name),(' '),(last_name)) as candidate_full_name");
Try like above 100% it will work in ci.
If cryptic solution doen't work then try it.
$query = "SELECT *
FROM (
SELECT user_id, user_telephone, user_email, CONCAT(user_firstname, ' ', user_surname) name
FROM users
) a
WHERE name LIKE '%".$term."%'";
$this->db->query($query);
Source: MySQL select with CONCAT condition
You have to SELECT the fields that you want concat like so:
$this->db->select('user_id, user_telephone, user_email, user_firstname, user_surname, CONCAT(user_firstname, '.', user_surname) AS name', FALSE);
$this->db->from('users');
$this->db->where('name', $term);
This will also solve the issue:
$this->db->select('user_id, user_telephone, user_email, user_firstname, user_surname, CONCAT(user_firstname,user_surname) AS name', FALSE);
$this->db->from('users');

Resources