How to write query using JDatabaseQuery in Joomla 1.7? - joomla

I have a following table and I want to know how to write the inset query using JDatabaseQuery in Joomla.
jos_esolutions_movies - table name
id, name, description, status - field names
1 , 'ABC' , 'ABC description', 0 - example values

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->insert('#__tablename');
$query->set("field1='values1', field2='values2'");
$db->setQuery($query);
$db->query();

the following link will help you to create a joomla database query http://docs.joomla.org/How_to_use_the_database_classes_in_your_script

$db =& JFactory::getDBO();
$query = "insert into #__esolutions_movies (id, name, description, status) values(1 , 'ABC' , 'ABC description', 0)";
$db->setQuery($query);
$db->query();

Related

Codeigniter 3 - Query Builder 'join' Method '!=' operator is not giving expected output

Not a Duplicate Question!!!
I am using CodeIgniter 3 - Query Builder Class with MySQLi.
Tables in DB:
'category_level_1' Table:
'category_level_2' Table:
Query in model.php:
$query = $this->db
->select('category_level_1.id, category_level_1.category')
->from('category_level_1')
->join('category_level_2', 'category_level_2.cat_lvl1_id != category_level_1.id', 'inner')
->group_by('category_level_1.id')
->get();
Output :
Inner-Join not working.
Expected Output :
Only need to output records in 'category_level_1' Table which are not related with 'category_level_2' Table.
Issue:
As showed above, output values are not as expected according to '!=' operator is not working with 'inner' join.
Hope this will help you :
$sql = "SELECT id, category
FROM category_level_1
WHERE id NOT IN (SELECT DISTINCT cat_lvl1_id FROM category_level_2)";
$query = $this->db->query($sql);
print_r($query->result());
Output :
Array
(
[0] => stdClass Object
(
[id] => 93
[category] => dummy
)
)
I suggest you try using a left orright join and a where clause. Give the following a go:
$query = $this->db
->select('category_level_1.id, category_level_1.category')
->from('category_level_1')
->join('category_level_2', 'category_level_2.cat_lvl1_id = category_level_1.id', 'left')
->where('category_level_2.cat_lvl1_id IS NULL')
->group_by('category_level_1.id')
->get();
$query = $this->db ->select('category_level_1.id, category_level_1.category') ->from('category_level_1') ->join('category_level_2', 'category_level_2.cat_lvl1_id <> category_level_1.id', 'inner') ->group_by('category_level_1.id') ->get();

How to use like clause in code iginiter query() WITH BIND PARAMS?

How to use like clause in code iginiter query() WITH BIND PARAMS?
Eg:
When I use
$query = 'SELECT mycol FROM mytable WHERE name LIKE %?';
$name = 'foo';
$db->this->query($query,array($name));
//the clause generated
//SELECT mycol FROM mytable WHERE id LIKE '%'foo'%'
//I expected this
//SELECT mycol FROM mytable WHERE id LIKE '%foo'
I don't to put param values inside query and use like below:
$query = 'SELECT mycol FROM mytable WHERE name LIKE '%foo';
Also I can not use $this->db->like() function as my query consists of:
INSERT IGNORE
and
INSERT INTO table SELECT col FROM table2;
Please suggests?
Thanks,
just use CodeIgniter Query Builder
$name = 'foo';
$query = $this
->db
->select('mycol')
->like('name', $name)
->get('mytable');
$query = 'SELECT mycol FROM mytable WHERE name LIKE ?';
$name = '%foo';
$this->db->query($query,array($name));
codeigniter will replace ? with 'params' value.
if you write this
$query = 'SELECT mycol FROM mytable WHERE name LIKE %?';
$name = 'foo';
//$db->this->query($query,array($name)); //you wrote this line wrong.
//it should be like this
$this->db->query($query,array($name));
it will produce
SELECT mycol FROM mytable WHERE name LIKE %'foo' //inverse comma after % ,actually before and after foo.
So your right way will be
$query = 'SELECT mycol FROM mytable WHERE name LIKE ?';
$name = '%foo';
$this->db->query($query,array($name));
It will produce
SELECT mycol FROM mytable WHERE name LIKE '%foo'
NOTE
You wrote this which is wrong
$db->this->query($query,array($name));
Right way
$this->db->query($query,array($name));

CodeIgniter parameterize integer and string

$code = Array(1,2,3,4)
$sql = "SELECT * FROM Table1 WHERE Field1 IN (?)";
$query = $this->db->query($sql, array($code));
$this->db->last_query() will show
"SELECT * FROM Table1 WHERE Field1 IN ('1,2,3,4')"
How can I remove the single quote in the IN condition?
Even if the $code is array of strings, example
$code = Array('This one', 'Next code', 'And this')
the statement will be:
"SELECT * FROM Table1 WHERE Field1 IN ('This one, Next Code, And This')"
Am I missing something ?
TIA.
You can use this simple and alternate way
$this->db->where_in('Field1',$code);
$this->db->get('Table1');
From codeigniter active record manual
$this->db->where() accepts an optional third parameter. If you set it
to FALSE, CodeIgniter will not try to protect your field or table
names with backticks.
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
So, put a 3rd parameter on a where clause with FALSE
Then your query should be
$this->db->select('*')
->where('Field1 IN('.$code.'),NULL,FALSE)
->get('Table1');

CodeIgniter and csv_from_result

I have getting data from a database and using
$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);
however I want to change the name of my headers for the CSV for first_name is First Name...how would I go about doing that?
Thanks,
J
Try renaming the columns returned by the sql query:
$query = $this->db->query("
SELECT
first_name as 'First Name', last_name as 'Last Name'
FROM mytable
");
The downside is that you will have to list the columns instead of a simple *.

"SELECT ... IN (SELECT ...)" query in CodeIgniter

I have a query similar to this:
SELECT username
FROM users
WHERE locationid IN
(SELECT locationid FROM locations WHERE countryid='$')
$ is a value I get from end user.
How could I run this query in CodeIgniter? I can't find a solution in CodeIgnite's user guide.
Thank you so much for your answers!
Regards!
Look here.
Basically you have to do bind params:
$sql = "SELECT username FROM users WHERE locationid IN (SELECT locationid FROM locations WHERE countryid=?)";
$this->db->query($sql, '__COUNTRY_NAME__');
But, like Mr.E said, use joins:
$sql = "select username from users inner join locations on users.locationid = locations.locationid where countryid = ?";
$this->db->query($sql, '__COUNTRY_NAME__');
Note that these solutions use the Code Igniter Active Records Class
This method uses sub queries like you wish but you should sanitize $countryId yourself!
$this->db->select('username')
->from('user')
->where('`locationId` in', '(select `locationId` from `locations` where `countryId` = '.$countryId.')', false)
->get();
Or this method would do it using joins and will sanitize the data for you (recommended)!
$this->db->select('username')
->from('users')
->join('locations', 'users.locationid = locations.locationid', 'inner')
->where('countryid', $countryId)
->get();
Also, to note - the Active Record Class also has a $this->db->where_in() method.
I think you can create a simple SQL query:
$sql="select username from user where id in (select id from idtables)";
$query=$this->db->query($sql);
and then you can use it normally.

Resources