Jump to content
MakeWebGames

Having A Stupid Moment (Arrays)


Seker

Recommended Posts

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 by Seker
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...