how to add two IDs in where condition in joomla query? - joomla

I want to add two IDs in where condition in joomla.
This is the my query.
$db->setQuery("SELECT content_item_id FROM #_contentitem_tag_map **WHERE tag_id = id1 and id2**");
How can i filter this two ids?

You can use the following:
$id_1 = XXX;
$id_2 = XXX;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('content_item_id')))
->from($db->quoteName('#__contentitem_tag_map'))
->where($db->quoteName('tag_id') . ' = ' . (int)$id_1, 'AND')
->where($db->quoteName('tag_id') . ' = ' . (int)$id_2 );
$db->setQuery($query);
You will notice here that I've used proper Joomla coding standard for your database query which should be used at all times. Using native mysql is not the way forward.
Hope this helps

Try this
$db->setQuery("SELECT content_item_id FROM #_contentitem_tag_map WHERE tag_id IN (id1,id2);
USE mysql IN
id IN (2,3,...)

Related

How to select all column from multiple tables in codeigniter using join?

I have 4 tables like this:
Now, I want to select all columns from all tables where model.featured=1. i.e.
model.id
model.name
model_attributes.id
model_attributes.attributes_value
model_images.id
model_images.model_images
attributes.id
attributes.name
attributes.value
I can only do basic level queries and I'm not sure if I'm anywhere near to the solution but this is what I have tried (returns nothing):
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model.id = model_images.model_id','RIGHT');
$this->db->join('model_attributes','model.id = model_attributes.model_id','RIGHT');
$this->db->join('attributes','model_attributes.attributes_id = attributes.id','RIGHT');
$this->db->where('model.featured', 1);
$query = $this->db->get();
return $query->result();
How do I achieve what I want ? Or, is there any other better methods to do it ?
// try this query
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model_images.model_id = model.id');
$this->db->join('model_attributes','model_attributes.model_id = model.id');
$this->db->join('attributes','attributes.id = model_attributes.attributes_id');
$this->db->where('model.featured','1');
$query = $this->db->get()->result();
return $query;
Please go through below mentioned solution.
$this->db->select('model.*,model_images.*,model_attributtes.*, attributes.*'):
Let me know if it not works for you.
Please go through below solution. If multiple table has same column name then you have to give alias for each column name which have same name in both the table. because by default CI merge all column name and generate result.
$this->db->select('*,c.name as c_name,s.name as s_name');
$this->db->from('country c');
$this->db->join('state s', 'c.id = s.country_id');
$query = $this->db->get();

Can not get the url parameter in PHP

I am trying to get URL parameter in SQL, but nothing happens.
Here is my URL:
http://localhost/webshop/imagegallery.php?categori=necklace
Here is my SQL query:
$sql = 'SELECT count(productid) FROM products where productcategori=".$_GET["categori"]"';
What am I doing wrong?
Have a look at this query, too:
$sql = 'select * from products join ids on products.productid=ids.productid join photos on photos.photosid=ids.photoid where products.productcategori='".$_GET["kategori"]."' && ids.photonumber=1 ORDER BY products.productid DESC $limit';
First of all, your quotation marks seem to be the problem. Try changing your query line to this:
$sql = "SELECT count(productid) FROM products where productcategori='".$_GET["categori"]."'";
Further, you should never insert variables into a SQL query like this. Never.
The reason is that like this, your system is vulnerable for SQL injections.
Instead consider using PDO. This SO question has a nice answer on how to do it correctly.
Using that answer, this is some example code regarding the last part of your question. Note that I replaced all variables in your query string by PDO placeholders.
<?php
$pdo = new PDO('mysql:dbname=mydatabase;host=127.0.0.1;charset=utf8', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM products JOIN ids ON products.productid=ids.productid JOIN photos ON photos.photosid=ids.photoid WHERE products.productcategori=:categori && ids.photonumber=1 ORDER BY products.productid DESC LIMIT :limit_min , :limit_max";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':categori', $_GET['categori']);
$stmt->bindParam(':limit_min', ($pagenum - 1) * $page_rows, PDO::PARAM_INT);
$stmt->bindParam(':limit_max', $page_rows, PDO::PARAM_INT);
$stmt->execute();
foreach($stmt as $row) {
// do something with $row
}
?>

How do you write a 'not in' select statement in Joomla 2.5 SQL?

Please explain how to write the commented out line in Joomla in the query below.
I can't figure it out using the documentation; that has only the simplest of queries.
function getUsersFromDatabase()
{
$db = JFactory::getDBO();
$query = $db -> getQuery(true);
$query
->select($db->quoteName(array('u.id', 'u.name', 'u.username', 'u.email')))
->from($db->quoteName('#__users', 'u'))
//->where($db->quoteName('u.id') . ' not in (select m.user_id from ' . $db->quoteName('#__user_usergroup_map', 'm') .' where ' . $db->quoteName('m.group_id') . ' in (4,19))') //no admins or superusers
->order($db->quoteName('u.name') . ' ASC');
$db ->setQuery($query);
$result = $db -> loadObjectList();
return $result;
}
You don't have to use Joomla's restrictive SQL querying platform to query your database. You can do the following:
$db = JFactory::getDBO();
$sql = "YOUR SQL QUERY";
$db->setQuery($sql);
$result = $db->loadAssocList();
Major extension, including K2, use the above straightforward method (they don't use Joomla's restrictive SQL).
Try this,
There is some alternate options like below,
$query
->select('*')
->from('products')
->where('category = ' . $db->q('catA'))
;
$q2
->select('*')
->from('products')
->where('category = ' . $db->q('catB'))
;
$query->union($q2);
$products = $db->setQuery($query)->loadObjectList();
There is some performance boostup with Joomla3.x using union. and it have some restriction over Joomla older versions.
Hope it helps..

Active records class in CodeIgniter

Basicly i have two tables photos and users. I wanna join tables and Update colums image_max and image_min. I get error unknown colum username. In which way i can join two tabels and get data from both. My sintax is:
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
And I get error
Unknown column username in where clause
UPDATE `photos` SET `image_max` = '', `image_min` = '' WHERE `username` = 'wwww'
apparently you need a letter on the table should say "users.username", check that.
Greetings.
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('users.username',$username);
$this->db->update('photos',$data);
You don't need to use "select and from" before upload fields, just update in this way
$data = array('image_max'=> 4, 'image_min' => 1);
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);

Filtering Results with CodeIgniter

I have two tables books and authors. Using CodeIgniter I would like users to be able to Filter results - for example, show results where author_id = 'x' and book_lang = 'y' and publication_year = '2000'. Filter criteria values come from user via drop-downs.
What's the best way to generate the query using codeigniter helper and binding etc?
For example, the query below will give me everything with author_id= 1. what if filters are a mix, for example; author_id = 1; language = 'all' (don’t put where clause on language) and publication year = 'all' also don’t put where clause.. Do I manually need to check for values and code or is there a codigniter helper method that allows filter to be removed from where clause if dropdown value is 'show all'?
$sql = "select * from AUTHORs a , BOOKS b where
a.AUTH_ID = b.BOOK_AUTH_ID
and b.BOOK_AUTH_ID = ?
and b.BOOK_LANGUAGE = ?
and b.PUB_YEAR = ? ";
$query = $this->db->query($sql, array ($author_id, $book_lang, $pub_year));
You should use Codeigniter's Active Record class to construct your query:
$this->db->select('*');
$this->db->from('AUTHORs a , BOOKS b');
$this->db->where('a.AUTH_ID', 'b.BOOK_AUTH_ID');
if ($author_id != 'all') $this->db->where('b.BOOK_AUTH_ID', $author_id);
if ($book_lang != 'all') $this->db->where('b.BOOK_LANGUAGE', $book_lang);
if ($pub_year != 'all') $this->db->where('b.PUB_YEAR', $pub_year);
$query = $this->db->get();
To read more about Codeigniter's Active Record class, visit the Docs:
http://codeigniter.com/user_guide/database/active_record.html

Resources