ashlegen Posted September 14, 2010 Share Posted September 14, 2010 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: $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($inventory)) .Thanks in advance and keep in mind that I'm a noob yet. Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/ Share on other sites More sharing options...
mds1256 Posted September 14, 2010 Share Posted September 14, 2010 Your SQL script is wrong, when using '%s' you need to use LIKE rather than = Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/#findComment-1110973 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 No, that's a formatted string placeholder, not a wildcard operator. Are you sure $userID has a valid value? Have you tried echo $query;? Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/#findComment-1110987 Share on other sites More sharing options...
mds1256 Posted September 14, 2010 Share Posted September 14, 2010 No, that's a formatted string placeholder, not a wildcard operator. Sorry your correct, i missed the 2nd part of the line below Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/#findComment-1110995 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2010 Share Posted September 14, 2010 What exactly are you trying to make that code chunk do? Is it expected to return multiple records from the database? Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/#findComment-1111031 Share on other sites More sharing options...
ashlegen Posted September 15, 2010 Author Share Posted September 15, 2010 Yes.I'm trying to get all of the item ids associated with a specific user id into an array.I'll keep you up to date and thanks for answering. Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/#findComment-1111303 Share on other sites More sharing options...
ashlegen Posted September 15, 2010 Author Share Posted September 15, 2010 No, that's a formatted string placeholder, not a wildcard operator. Are you sure $userID has a valid value? Have you tried echo $query;? Oh man!I checked now and i've got the poblem.You were right.$uerID never had a value because i forgot the session_start() thing at the begiing of the document.Thanks again guys and I'm looking forward to ask more here.Cheers! Link to comment https://forums.phpfreaks.com/topic/213383-cant-see-the-problem-in-a-simple-php-code/#findComment-1111313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.