I'm trying a php code but i don't know what's wrong!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashlegen
    New Member
    • Sep 2010
    • 1

    I'm trying a php code but i don't know what's wrong!

    Hi guys.I've got a problem.I'm building my php browsergame and i'm stuck into the inventory page.I have a code that seems ok to me but it won't work.Here it is:


    Code:
    $inventory = array();
    $query = sprintf("SELECT id, item_id, quantity FROM user_items WHERE user_id = '%s'",
    		mysql_real_escape_string($userID));
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)) {
    	 $item_query = sprintf("SELECT name FROM items WHERE id = '%s'",
    		mysql_real_escape_string($row['item_id']));
    	$item_result = mysql_query($item_query);
    	list($row['name']) = mysql_fetch_row($item_result);
    	array_push($inventory,$row);
    }


    I've spotted the problem.The $inventory array is empty.I checked it with if(empty($inven tory)) .Thanks in advance and keep in mind that I'm a noob yet.
    Last edited by Dormilich; Sep 14 '10, 06:42 PM. Reason: please use [code] [/code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I think you’re better off with a JOIN.
    Code:
    SELECT items.name FROM items
    JOIN user_items ON user_items.item_id = items.id
    WHERE user_items.user_id = ?
    after that you can get the result by using PDO and its fetchAll() method

    Comment

    Working...