Hi there,

I am relatively new to PHP and MySQL coding and I am currently using the Joomla CMS.

What I am trying to do is get a game name based on a custom field value from my DB like so;

// fetch the value of field called Game
$gname = $custom[0]->value; //returns something like Trine 2
//connect to database
$database    =& JFactory::getDBO();
$query  = "SELECT name, id, avatar FROM jos_games WHERE name = '$gname'";
$result = mysql_query($query);
// return database as arrays
while($row = mysql_fetch_array($result)){
echo $row["name"]; //this too should return the name Trine 2 if it exists

it is far from perfect and I am not sure (as I am still learning) but I think it has something to do with $custom[0]->value; because if I change

$gname = $custom[0]->value;


$gname = 'Trine 2'; //changed to string

then the code works fine, please help me out :)

Recommended Answers

All 21 Replies

show us the root of this, and we will tell you.


the root could be class or function..

It appears to be a problem with your $custom[0]->value indeed. Have you tried to use


to see what's actually in $custom?

Well I did the dump and here is what it returned

array(6) { [0]=> object(stdClass)#550 (7) { ["id"]=> string(1) "3" ["name"]=> string(4) "Game" ["value"]=> string(81) "Trine 2 " ["type"]=> string(6) "labels" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "1" } [1]=> object(stdClass)#551 (7) { ["id"]=> string(1) "4" ["name"]=> string(5) "Genre" ["value"]=> string(10) "Platformer" ["type"]=> string(14) "multipleSelect" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "2" } [2]=> object(stdClass)#552 (7) { ["id"]=> string(1) "5" ["name"]=> string(9) "Developer" ["value"]=> string(87) "Frozenbyte " ["type"]=> string(6) "labels" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "3" } [3]=> object(stdClass)#553 (7) { ["id"]=> string(1) "6" ["name"]=> string(9) "Publisher" ["value"]=> string(77) "Atlus " ["type"]=> string(6) "labels" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "4" } [4]=> object(stdClass)#554 (7) { ["id"]=> string(1) "7" ["name"]=> string(15) "Reviewer Rating" ["value"]=> string(5) "4 / 5" ["type"]=> string(5) "radio" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "5" } [5]=> object(stdClass)#555 (7) { ["id"]=> string(1) "8" ["name"]=> string(10) "GamersGate" ["value"]=> string(98) "Purchase On GamersGate" ["type"]=> string(4) "link" ["group"]=> string(1) "1" ["published"]=> string(1) "1" ["ordering"]=> string(1) "6" } }

I am trying to use

["name"]=> string(4) "Game" ["value"]=> string(81) "Trine 2 "

from all of that WHERE name = the string value.

So anyone know what I can do to make this work? :)

shouldn't it be something like echo $row->value; try also

while($row = mysql_fetch_assoc($result)){
    echo $row["name"]; //would return game based on your dump

thats is correct

echo $row["name"];

returns a whole range of game names from my database but

echo $custom[0]->value;

returns only what the article writer has typed to be the 'related game' to the article on my site.

I need to fetch the information from my database about the 'related game' based on what ever string



if I echo them alone to display the value they both work but if I use the (i think its called an array but im new to php so not too sure) $custom[0]->value; as WHERE like so;

// fetch the value of field called Game
//connect to database
$database    =& JFactory::getDBO();
$query  = "SELECT name, id, avatar FROM jos_games WHERE name = '$custom[0]->value'"; //should SELECT name, id and avatar WHERE name = Trine 2

then WHERE name = '$custom[0]->value' will not fetch Trine 2 even though $custom[0]->value echos (orreturns) 'Trine 2'.

I am so confused argh lol

your new to PHP? but your doing joomla? for me, going directly to CMS is a bad choice for starters, it's better if you 1st familiarize yourself with the basic and native php before you go advance. joomla is framework and it's coding structure is not just typical function structures you see everyday....joomla is an Object Oriented Framework

new was probably the wrong context, im more "still learning" Ive done much coding for joomla but dont understand why this code doesn't work.

To be fair this is the first time I have posted for help with coding as I dont under stand how echo $custom[0]->value; returns the game name on its own but when used in the code above it seems like its returning NULL.

if you echo out $query what do you get?

oh right and shouldn't you be passing in $database $result = mysql_query($query, $database); have you checked to see if you are getting any mysql errors

$result = mysql_query($query, $database);
if (!$result) {
    die('Invalid query: ' . mysql_error());

Thanks jstfsklh211 for making me look at the way I was connecting to the Database haha I totally blanked it as I was blinded by the current issue, I have fixed to code up but still no luck, take a look at this;

$custom = $this->item->extra_fields;
$gname = $custom[0]->value;
$gname2 = 'Killzone 3';

$db =& JFactory::getDBO();
$query = "SELECT id, name, avatar FROM #__community_groups WHERE name='$gname'"; // If I replace $gname with $gname2 then it works a treat :( 

$db->setQuery( $query );
$game = $db->loadObject();
echo $game->id;

as I mention in the code, if I use $gname2 instead of $gname then it works and I have no idea why, I really need advice on this!
Im stalking this page for replies hahaha!Thanks :)

if you echo $gname; what do you get
if you echo $custom[0]->value; what do you get
also try

$custom = custom[0];
echo $custom->value

if you echo $gname; what do you get
if you echo $custom[0]->value; what do you get
also try

$custom = custom[0];
echo $custom->value

Still no luck using

$custom = custom[0];
echo $custom->value

Here is what they return

echo $custom[0]->value; // returns the correct game name
echo $gname; // returns the correct game name

it just wont work when I use it in the query

try $temp = $custom[0]->value; and use $temp in the query

are you sure you're executing the right query?
if you output $query what do you get

[codei]$temp[/codei] didnt work either.

echo $query; // returns SELECT id, name, avatar FROM #__community_groups WHERE name='Trine 2 '

so the query returns correct - it doesnt make sense argh lol

um there us an extra space after 'trine 2 ' thats your problem
sql will check for a value containing that extra space i belive
try trim($gname) in your query

okay the trim is in place as $gnameTrim = trim($gname); and the query is now returning SELECT id, name, avatar FROM #__community_groups WHERE name=Trine 2
I am getting the error of "Notice: Trying to get property of non-object" on the line of echo "id = " . $game->name;

it may be worth mentioning (as I have only just notices) that $gnameTrim returns the value of Trine 2 but it is also a clickable link, the HTML code it returns is <a href="link to search">Trine 2</a>

not sure if that matters but anyway it still isnt returning the information from the database even though the Querys output is correct. :S

post var_dump($gname)

var_dump($gname) returns string(81) "Trine 2 " so I am guessing its something to do with string(81)?

oops i meant var_dump($game) the result from the query

hehe right done the var_dump($game); and it returns NULL , its strange to me why this is happening as echo $query; is returning the correct string.

Okay good news, I got it working. The issue seems to be that the 'Extra Field' was a Searchable field and I had to change it to a plain old Text Field.

The help on these forums have been AMAZING to say the least so expect more posts from lil ol' ME!

Thanks again :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.