Seker Posted August 15, 2012 Share Posted August 15, 2012 (edited) I've had to completely create a new donation system due to the major overhaul I did of the items system. I've run into a snag. I obviously haven't grasped arrays like I was hoping and I'm looking for some tips, if they're there to give. What I'm trying to do is avoid a ton of if statements on displaying items within the donator packs. I thought I could use some arrays to pick the right table for each item type. Here's my section of code: $pitemtype = $ap['pack_item_type']; $pitemid = $ap['pack_item_id']; $tablearray = array(1 => 'weapons', 2 => 'weapons', 3 => 'armor', 4 => 'energy_items', 5 => 'health_items'); $idarray = array(1 => 'weapon_id', 2 => 'weapon_id', 3 => 'armor_id', 4 => 'energy_item_id', 5 => 'health_item_id'); $tablename = $tablearray[$pitemtype]; $idname = $idarray[$pitemid]; $packitemquery = sprintf("SELECT * FROM `$tablename` WHERE (`$idname`=%d)", $pitemid, $c); $packitem = mysql_query($packitemquery); $pit = mysql_fetch_array($packitem); And here's the error I get: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in *****/donator.php on line 137 I'm just not entirely sure where I've gone wrong. As I said, I must not understand arrays, yet. I have only just begun to play around with arrays. Any ideas? And, by the way, if it's needed, line 137 is just declaring the $pit variable. In case that wasn't obvious. Edited August 15, 2012 by Seker Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted August 15, 2012 Share Posted August 15, 2012 Could you print out your query string $packitemquery and show us what it contains? Also, in your sprintf, I see only one place older %d which will be replaced with the value $pitemid, but $c? I don't see. Quote Link to comment Share on other sites More sharing options...
Seker Posted August 15, 2012 Author Share Posted August 15, 2012 Could you print out your query string $packitemquery and show us what it contains? Also, in your sprintf, I see only one place older %d which will be replaced with the value $pitemid, but $c? I don't see. No need. I found it. As usual, with these types of errors, it was just an idiot move on my part. The $idname variable was trying to select columns by using the item id instead of the item type. Which made no sense. Figures, I figured it out within seconds of posting. That $c is a V1 thing. I suppose I should format it as well. I think, for some reason, I thought it unnecessary. It connects the query to the DB. Hah. Come to think of it. That's probably one of the more important parts. Thanks for bringing that to my attention. Quote Link to comment Share on other sites More sharing options...
a_bertrand Posted August 15, 2012 Share Posted August 15, 2012 $c cannot connect the query to the database as the sprintf function just produce a string and has basically nothing to do with the database. In case you need the connection then you need to pass it to the mysql_query as second parameter. If you don't (as you are doing) it's basically re-use the last opened connection. Quote Link to comment Share on other sites More sharing options...
Seker Posted August 15, 2012 Author Share Posted August 15, 2012 $c cannot connect the query to the database as the sprintf function just produce a string and has basically nothing to do with the database. In case you need the connection then you need to pass it to the mysql_query as second parameter. If you don't (as you are doing) it's basically re-use the last opened connection. Ah Okay, yeah, I see what you're saying. Will be sure to fix that. Thanks. Quote Link to comment Share on other sites More sharing options...
Djkanna Posted August 15, 2012 Share Posted August 15, 2012 As you found out; "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in *****/donator.php on line 137" means your query (the one that you're passing the resource of to mysql_fetch_array()) failed, and returned a boolean value (afaik, mysql_query returns false on failure). Check the query doesn't fail beforehand (you can do this multiple ways). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.