Friday, May 24, 2013
   
Text Size
Login

JDatabase: using the Joomla database

Sorry just a quick note for myself on accessing the Joomla database using the Joomla classes. Source: Docs joomla org

Basic Query - 1 result

  1.  $db = JFactory::getDBO(); 
  2.  $query = " 
  3.    SELECT ".$db->nameQuote('field_name')." 
  4.      FROM ".$db->nameQuote('#__my_table')." 
  5.      WHERE ".$db->nameQuote('some_name')." = ".$db->quote($some_value); 
  6.  $db->setQuery($query); 
  7.  $result = $db->loadResult(); 
  8.   
  9.  -- $result = value of field_name 

LoadRow / LoadAssoc

  1.  $query="SELECT username FROM users WHERE name='me'"; 
  2.  $db->setQuery($query); 
  3.  $row = $db->loadRow(); // $row = $db->loadAssoc(); 
  4.   
  5.  -- $row['username'

loadObject

  1.  $query="SELECT username FROM users WHERE name='me'"; 
  2.  $db->setQuery($query); 
  3.  $row = $db->loadObject(); 
  4.   
  5.  -- $row->username 

loadRowList / loadAssocList

  1.  $query="SELECT username FROM users WHERE name='me'"; 
  2.  $db->setQuery($query); 
  3.  $rows = $db->loadRowList();  // $db->loadAssocList(); 
  4.   
  5.  -- $rows['0']['username'
  6.   
  7.  -- foreach( $rows as $row ) { 
  8.  --     $this_user = $row['username']; 
  9.  --     $user_list_string.= "<li>$this_user</li>"; 
  10.  -- } 

loadObjectList

  1.  $query="SELECT username FROM users WHERE name='me'"; 
  2.  $db->setQuery($query); 
  3.  $rows = $db->loadObjectList(); 
  4.   
  5.  -- $rows['2']->username 
  6.   
  7.  -- foreach( $rows as $row ){ 
  8.  --     $this_user = $row->username; 
  9.  --     $user_list_string.= "<li>$this_user</li>"; 
  10.  -- } 

getNumRows

  1.  $query="SELECT username FROM users WHERE name='me'"; 
  2.  $db->setQuery($query); 
  3.  $db->query(); 
  4.  $num_rows = $db->getNumRows(); 
  5.  print_r($num_rows); 
  6.  $result = $db->loadRowList(); 
  7.   
  8.  -- eg. 3 
  9.  -- NOTE: Load row list after get num rows or PHP warning: 
  10.  -- Warning: mysql_num_rows(): 80 is not a valid MySQL result resource  
  11.  -- in \joomla\database\database\mysql.php on line 344 

Add Comment

Name:

Email:

Website:

Message:



Human Check:

Security code
Refresh

Please type what you see in the image above:

Latest Posts

  • Joes Revolver Map (JRM)

    • Fri 17-May-13
      Hmmm... Sounds like a problem with the identifier. Was it working before and has there been a change ...
      Joel Lipman  
    • Fri 17-May-13
      Hello Joel: Yes, I do have it published on all pages of the site. I just went back to Revolver maps to ...
      Bill Duncan  
    • Fri 17-May-13
      Hi Bill, From the developers of RevolverMaps, "the module would need to be published on every page ...
      Joel Lipman  
    • Fri 17-May-13
      Hi Bill, I'll investigate further as you're not the first to say this happens. In the meantime, simply ...
      Joel Lipman  
    • Fri 17-May-13
      Its a great extension. But when I set it up I only show my presence on the 3D map and no other visitors ...
      Bill Duncan