Simple Joomla / MySql question

115 views
Skip to first unread message

Christian McCabe

unread,
Feb 16, 2013, 1:18:13 PM2/16/13
to joomla-de...@googlegroups.com
I have been struggling trying to translate a PHP mysql query to Joomla. Not sure why it is not working but I am simply trying to search a table and return only the rows that match a string or $variable value. For some reason I can't get it working with any of the PHP syntax examples that I have found. 

Could someone please give me the syntax to search a database in Joomla. Here is what I have:

$subject = "design";
$db = JFactory::getDBO();
$query = $db->getQuery(true);

$query->select('course_html');
$query->from('#__ws_courses');
$query->where('course_subject LIKE '$subject' ' );
$db->setQuery($query);
$rows = $db->loadObjectList();

I either get no result or a php error when I try different syntax for the WHERE LIKE statement is... 



--

Kind Regards,

Christian McCabe

Donald Gilbert

unread,
Feb 16, 2013, 1:20:07 PM2/16/13
to joomla-de...@googlegroups.com
change your query up like this:

$query
    ->select('course_html')
    ->from('#__ws_courses')
    ->where("course_subject LIKE '%{$subject}%' ");

$rows = $db->setQuery($query)->loadObjectList();


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Olivier Nolbert

unread,
Feb 16, 2013, 1:21:09 PM2/16/13
to joomla-de...@googlegroups.com
Hi,

Try this (not tested) :

$subject = "design";
$db = JFactory::getDBO();
$query = $db->getQuery(true);

$query->select('course_html');
$query->from('#__ws_courses');
$query->where('course_subject LIKE '.$db->quote($subject) );
$db->setQuery($query);
$rows = $db->loadObjectList();


2013/2/16 Christian McCabe <see...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Olivier Nolbert, Jiliko
14 passage Lecroisey 76600 Le Havre, France
SIREN : 517 564 365

Donald Gilbert

unread,
Feb 16, 2013, 1:23:11 PM2/16/13
to joomla-de...@googlegroups.com
You need the % in there to do partial matching (the purpose of LIKE). Meaning, if the course_subject was "Web Design" then what i wrote would match that. However, if you are wanting exact match then you should drop the LIKE and replace it with =

Christian McCabe

unread,
Feb 16, 2013, 1:36:56 PM2/16/13
to joomla-de...@googlegroups.com
@donald

Your suggestion was just what I needed. It worked like a charm! Thanks
Kind Regards,

Christian McCabe

hoochicken

unread,
Feb 18, 2013, 2:15:50 AM2/18/13
to joomla-de...@googlegroups.com
Hi,
know it's already solved, but I got a question
with "setQuery()" one loads the query; with "query()" it is executed:

$db->setQuery($query); /*Sets the SQL statement string for later execution, see: http://docs.joomla.org/JDatabase::setQuery/11.1*/
$db->query(); /*Execute the SQL statement. see: http://docs.joomla.org/JDatabase::query/11.1*/
$rows = $db->loadObjectList();

Question: Did it work without the "query()"? If yes, somebody knows why?
Greetings
Mareike

Christian McCabe

unread,
Feb 18, 2013, 2:24:02 AM2/18/13
to joomla-de...@googlegroups.com
Yes it worked with the following code:

$subject = $params->get('message');
$db = JFactory::getDBO();
$query = $db->getQuery(true);

$query->select('*')
->from('#__ws_courses')
    ->where("course_subject LIKE '%{$subject}%' ");
$db->setQuery($query);
$rows = $db->loadObjectList();

Not sure why, given the instructions you posted. Maybe someone else can let us know why. But I can confirm that this module with this code is working as expected and calling from the database.


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

hoochicken

unread,
Feb 18, 2013, 2:46:03 AM2/18/13
to joomla-de...@googlegroups.com
Hi,
thanks for the reply.
So looked up in the source code (http://hg.joomla.org/joomla-platform/src/247ba8d88526/libraries/joomla/database/database.php#cl-699);
seems in loadObjectList() the query() method is executed automatically.

        // Execute the query and get the result set cursor.
        if (!($cursor = $this->query())) return null;
       
So when using loadObjectList() setting the Query is enough:-)
Greetings
Mareike

Christian McCabe

unread,
Feb 18, 2013, 2:47:55 AM2/18/13
to joomla-de...@googlegroups.com
AH! Nice find. I am just learning and this is the first time I used the loadObjectList() - I saw it used in another component and started there.





Greetings
Mareike

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Herman Peeren

unread,
Feb 18, 2013, 2:57:36 AM2/18/13
to joomla-de...@googlegroups.com
Generally you use loadObjectList() to execute a select-query (and get a result-set back). The query()-method executes any query and is generally used for updates, inserts and deletes. 
Reply all
Reply to author
Forward
0 new messages