Retrieve data from database

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.
Locked
User avatar
robilro1
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 112
Joined: Thu Nov 22, 2007 1:10 pm
Location: Romania
Contact:

Retrieve data from database

Post by robilro1 » Thu Jul 17, 2008 5:18 pm

Hi!

How do I retrieve data from a table in Joomla 1.5 without using MCV?

Thank you!
Robintel BMS.
"Cine o cauta suficient o gaseste!" ("The one looking for it long enough, will find it!")

User avatar
carsten888
Joomla! Ace
Joomla! Ace
Posts: 1224
Joined: Sat Feb 11, 2006 8:32 am
Contact:

Re: Retrieve data from database

Post by carsten888 » Sun Jul 20, 2008 11:43 am

I don't know what you mean with MCV, but this is the code i always use:

Code: Select all

$db = JFactory::getDBO();

$query = "SELECT content_id"
. " FROM #__content_frontpage"
. " WHERE content_id = $item_id"
. " LIMIT 1"
;
$db->setQuery($query);
$rows = $db->loadObjectList();
$itemrow = $rows[0];
$on_frontpage = $itemrow->content_id;
http://www.pages-and-items.com my extensions:
User-Private-Page, Redirect-on-Login, Admin-Help-Pages, Dynamic-Menu-Links, Admin-Menu-Manager, plugin load module in article, plugin pure css tooltip and more...

User avatar
robilro1
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 112
Joined: Thu Nov 22, 2007 1:10 pm
Location: Romania
Contact:

Re: Retrieve data from database

Post by robilro1 » Mon Jul 21, 2008 11:03 am

Hi!

Thanks.

My second question is: what do these lines mean?

Thanks again.
Robintel BMS.
"Cine o cauta suficient o gaseste!" ("The one looking for it long enough, will find it!")

User avatar
carsten888
Joomla! Ace
Joomla! Ace
Posts: 1224
Joined: Sat Feb 11, 2006 8:32 am
Contact:

Re: Retrieve data from database

Post by carsten888 » Mon Jul 21, 2008 3:28 pm

My second question is: what do these lines mean?
er... :geek:

make an instance of the database-object (joomla 1.5.x only)

Code: Select all

$db = JFactory::getDBO();
make a query to select something from the database.

Code: Select all

$query = "SELECT content_id"
. " FROM #__content_frontpage"
. " WHERE content_id = $item_id"
. " LIMIT 1"
;
execute the query

Code: Select all

$db->setQuery($query);
get the results of the query back as a object

Code: Select all

$rows = $db->loadObjectList();
in this bit of code I only needed the data from one row

Code: Select all

$itemrow = $rows[0];
read value from some table's row's column

Code: Select all

$on_frontpage = $itemrow->content_id;
:)

hope that helps
http://www.pages-and-items.com my extensions:
User-Private-Page, Redirect-on-Login, Admin-Help-Pages, Dynamic-Menu-Links, Admin-Menu-Manager, plugin load module in article, plugin pure css tooltip and more...

User avatar
robilro1
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 112
Joined: Thu Nov 22, 2007 1:10 pm
Location: Romania
Contact:

Re: Retrieve data from database

Post by robilro1 » Tue Jul 22, 2008 9:12 am

:D That helps, thanks! :D

My "not so bright" question is: does the DB instance ($db = JFactory::getDBO();) work with PostgreSQL too, or this is only used to retrieve data from the Joomla MySQL database?

:pop

Thanks!
Robintel BMS.
"Cine o cauta suficient o gaseste!" ("The one looking for it long enough, will find it!")

User avatar
carsten888
Joomla! Ace
Joomla! Ace
Posts: 1224
Joined: Sat Feb 11, 2006 8:32 am
Contact:

Re: Retrieve data from database

Post by carsten888 » Tue Jul 22, 2008 1:19 pm

does the DB instance...work with PostgreSQL too
sorry, don't know. maybe you should test that. ;)
http://www.pages-and-items.com my extensions:
User-Private-Page, Redirect-on-Login, Admin-Help-Pages, Dynamic-Menu-Links, Admin-Menu-Manager, plugin load module in article, plugin pure css tooltip and more...


Locked

Return to “Joomla! Coding 101”