MYSQL query help

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
zumadi
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Jul 10, 2006 1:15 pm

MYSQL query help

Post by zumadi » Sat Feb 13, 2016 12:51 pm

Hello,

Code: Select all

$id="2";


$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('column');
$query->from($db->quoteName('#__table_name'));
$query->where($db->quoteName('id')." = ".$id);
 
$db->setQuery($query);
$db->execute();
$result = $db->getNumRows();

What is wrong here? $id
$query->where($db->quoteName('id')." = ".$id);

Thanks

User avatar
sudo-web
Joomla! Ace
Joomla! Ace
Posts: 1325
Joined: Fri Jan 22, 2016 7:10 pm
Location: Vienna - Austria
Contact:

Re: MYSQL query help

Post by sudo-web » Sat Feb 13, 2016 1:08 pm

Use this format:

Code: Select all

$query->where($db->quoteName('id')." = ".$db->quote($id));
You find more details and examples in the documentation:
https://docs.joomla.org/Selecting_data_using_JDatabase
Visit me on my Webdesign Webpage: https://www.posit.at

zumadi
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Jul 10, 2006 1:15 pm

Re: MYSQL query help

Post by zumadi » Sun Feb 14, 2016 12:35 pm

Thank you for the answer,

This is working

Code: Select all

		$db = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->select('column_name');
		$query->from($db->quoteName('#__table_name'));
		$query->where($db->quoteName('id')." = ".$db->quote($id));
 
		$db->setQuery($query);
		$db->execute();
		$rr = $db->loadColumn();
  
		foreach ($rr as $sult) {
    		$result = $sult;
		}
I want one result, Why am I getting array?
What is wrong?

User avatar
sudo-web
Joomla! Ace
Joomla! Ace
Posts: 1325
Joined: Fri Jan 22, 2016 7:10 pm
Location: Vienna - Austria
Contact:

Re: MYSQL query help

Post by sudo-web » Sun Feb 14, 2016 7:08 pm

Because a Query could have more than one result. We both know that the id is unique and you only select one Colum, so it is logic for us the there should be only one result, but sometimes things are different, and you get no result or more than one.
Visit me on my Webdesign Webpage: https://www.posit.at


Locked

Return to “Joomla! 3.x Coding”