DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Need Help with converting an ID to a Name (http://www.daniweb.com/forums/thread92062.html)

jtmcgee Oct 7th, 2007 5:49 pm
Need Help with converting an ID to a Name
 
        if( $rand_num <= $chance )
                {
                        //$item_id = mysql_query("SELECT `item` FROM `creatures` WHERE id = '$id_creature'");
                        $foo =& creature_stats( item, $id_creature );
                       
                        $item_query = mysql_query("SELECT * FROM items WHERE id = '$foo'") or ('$item_query');

                        $item = mysql_fetch_array($item_query);

                        echo"You got an ".$item."!";

                }

Creature Stats Function:

function &creature_stats($what_stat, $id) 
{
$mysql_query=mysql_query("SELECT * FROM `creatures` WHERE `id` = '$id'");
$info=mysql_fetch_array($mysql_query);
return $info[$what_stat];
}

I need help getting it to echo $item

It justs puts out nothing.

kkeith29 Oct 7th, 2007 8:00 pm
Re: Need Help with converting an ID to a Name
 
try this, it might work.

if( $rand_num <= $chance )
                {
                        $item_id = mysql_query("SELECT `item` FROM `creatures` WHERE id = '$id_creature'");
                        $foo =& creature_stats( item, $id_creature );
 
                        $item_query = mysql_query("SELECT * FROM items WHERE id = '$foo'") or ('$item_query');
 
                        $item = mysql_fetch_row($item_query);
 
                        echo"You got an ".$item[0]."!";
 
                }

johny_d Oct 8th, 2007 5:39 pm
Re: Need Help with converting an ID to a Name
 
Quote:

$item = mysql_fetch_array($item_query);
means $item is an array an you cannot put an array in an echo statement; this is why you get nothing.
You need to use $item['name_of_mysql_column'] in your "echo...."
You also have a sintax error: in
Quote:

$foo =& creature_stats( item, $id_creature );
since item is a variable, it needs a $ sign in front; this is a very good reason why you get a blank nothing :)

kkeith29 Oct 8th, 2007 5:53 pm
Re: Need Help with converting an ID to a Name
 
sorry i overlooked that sintax error. wow i can't believe i missed that. also, before posting the code i did i should of asked the question; "Will the query ever return more than one result?". i figured there would only be one result and my way would work perfectly, but if more than one is returned you would need to use johny_d's way.


All times are GMT -4. The time now is 9:21 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC