SQL query with joomla - joomla

I have problem with my SQL query.
I try to eject these queries and the result is nothing.
$query =$db->getQuery(true);
$query->select($db->quoteName('name'));
$query->from($db->quoteName('#__restaurant'));
$query->where($db->quote(constant("$vector[0]")) . '=' . $db->quote($v[$vector[0]]));
$db->setQuery($query);
$column=$db->loadObjectList();
.......
$query= $db->getQuery(true);
$query->select($db->quoteName('name'));
$query->from($db->quoteName('#__restaurant'));
$query->where($db->quoteName('zone'). '=' . $db->quoteName('atocha'));
$db->setQuery($query);
$column=$db->loadObjectList();
The database is not empty, but I why there is no results.
Any idea!!!
thanks
Thanks everyone I execute this
$query= "SELECT *
FROM ".$db->nameQuote('#__restaurantes')."
WHERE ".$db->nameQuote('tipe')."=".$db->quote($v[$vector[0]]).";";
$db->setQuery($query);
$column=$db->loadObjectList();
and obtain this
SELECT * FROM #__restaurantes WHERE tipe='';sol
any idea
thanks

I think you are going a little owerboard with your quote() and quoteName() functions.
Try this:
$query= $db->getQuery(true);
$query->select('name');
$query->from('#__restaurant');
$query->where('zone = "atocha"');
$db->setQuery($query);
$column = $db->loadObjectList();
Also, please post the resulting SQL if this does not work. You can get it by:
echo $db->getQuery();

Thanks you so much everyone !
I have the correct query, I put here for some one who has the same problem.
$db=& JFactory::getDBO();
$query= "SELECT *
FROM ".$db->nameQuote('#__restaurant')."
WHERE ".$db->nameQuote(constant("$vector[0]"))."=".$db->quote($v[$vector[0]]).";";
$db->setQuery($query);
$column=$db->loadObjectList();
return $column;

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 to add two IDs in where condition in joomla query?

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,...)

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..

writing the query in codeigniter giving a different result than required

I am working on codeigniter and I am writing the following query:
$this->db->select('*');
$this->db->from('tbl_profile');
$this->db->join('tbl_msg', 'tbl_msg.msg_sender_id = tbl_profile.profile_id');
$this->db->order_by("msg_id", "desc");
$query = $this->db->get('', $config['per_page'], $this->uri->segment(3));
$data['records'] = $query->result_array();
Correspondingly I am getting the following result:
SELECT (*) FROM tbl_profile
JOIN tbl_msg ON tbl_msg.msg_sender_id = tbl_profile.profile_id
Which is returninng a wrong result as I want the result corresponding to the following query:
select * from tbl_profile as A
join (select * from tbl_msg) as B on A.profile_id = B.msg_sender_id
Please help
First of all, you missing the order by clause, but I assum, you mean other differences.
If you want that, you can use this query, what will gave you back the exact code:
$this->db->select('*', FALSE);
$this->db->from('tbl_profile as A');
$this->db->join('( select * from tbl_msg ) as B', 'A.msg_sender_id = B.profile_id');
$this->db->order_by("msg_id", "desc");
$query = $this->db->get('', $config['per_page'], $this->uri->segment(3));
$data['records'] = $query->result_array();
From codeigniter user manual:
$this->db->select()
accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

Resources