PHP coding error accessing database!

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
User avatar
donnan
Joomla! Ace
Joomla! Ace
Posts: 1494
Joined: Sat Jun 30, 2007 1:23 am
Location: Sydney, Australia
Contact:

PHP coding error accessing database!

Post by donnan » Sun Jun 24, 2018 4:28 am

Hi guys,

I am trying my hand at using the joomla way of accessing my database records but not having much luck.

I have a "while" loop that I need to execute to display each of my records from the database.

Can someone please help me with the correct code to achieve this.

This is what I have tried without success.

I did try the "while" loop but commented it out and tried the "foreach"...but it's no good either.

Code: Select all

 $db->setQuery($query);
$row = $db->loadAssoc();


//while($row = mysqli_fetch_array($row)) {

		foreach($query->result() AS $row) {


echo "Record".$row['id'];
echo "Name".$row['name'];
}
I think I am probably way off, but I am trying. :)

This is the error I am getting: " Call to a member function result() on string "
Last edited by toivo on Sun Jun 24, 2018 6:39 am, edited 2 times in total.
Reason: mod note: marked as solved

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17427
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP coding error accessing database!

Post by toivo » Sun Jun 24, 2018 5:49 am

Here are two ways to access the rows and columns:

Code: Select all

$rows  = $db->loadAssocList();
foreach($rows AS $row)
{
	echo $row['id'] . ' ' . $row['name'] . PHP_EOL;
}

Code: Select all

$rows  = $db->loadObjectList();
foreach($rows AS $row)
{
	echo $row->id . ' ' . $row->name . PHP_EOL;
}
Toivo Talikka, Global Moderator

User avatar
donnan
Joomla! Ace
Joomla! Ace
Posts: 1494
Joined: Sat Jun 30, 2007 1:23 am
Location: Sydney, Australia
Contact:

SOLVED Re: PHP coding error accessing database!

Post by donnan » Sun Jun 24, 2018 6:32 am

Thank you so much toivo.

Did the trick nicely. :)

User avatar
donnan
Joomla! Ace
Joomla! Ace
Posts: 1494
Joined: Sat Jun 30, 2007 1:23 am
Location: Sydney, Australia
Contact:

Re: PHP coding error accessing database!

Post by donnan » Sun Jun 24, 2018 6:49 am

Must I use this line EVERY time I want to access the database?

$db = JFactory::getDbo();

I wish to get some more information from another table while in the 'foreach' loop.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17427
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: PHP coding error accessing database!

Post by toivo » Sun Jun 24, 2018 8:19 am

Not necessarily every time, normally just once in each script. It is possible to pass the $db object as a parameter to a function defined in a different script, but that does not reduce overheads because the getDbo() function creates a new JDatabaseDriver object only when it is called the first time in a PHP session.

However, if the script sets up a number of different queries, it is good practice to clear the query object before the next query is built:

Code: Select all

$query->clear();
If you build a new query inside the results loop, be careful to use a different variable for the second level of results, for example $rows2.

It may also be possible to combine the second query with the first using JOIN, in which case only one query is run.
Toivo Talikka, Global Moderator

User avatar
donnan
Joomla! Ace
Joomla! Ace
Posts: 1494
Joined: Sat Jun 30, 2007 1:23 am
Location: Sydney, Australia
Contact:

Re: PHP coding error accessing database!

Post by donnan » Sun Jun 24, 2018 9:03 am

thanks toivo so much.

I am just learning how to program a little more the way joomla does it and it's certainly a big learning curve for me.

I appreciate you taking the time to help me out. :)


Locked

Return to “Joomla! 3.x Coding”