Forum Moderators: coopster

Message Too Old, No Replies

problem with if, while

         

dkin

2:03 am on May 2, 2005 (gmt 0)

10+ Year Member



I am trying to get my script working but as usual I seem to be one step behind the bugs.

what I want my code to do is this certain portion credits a user by giving him an item if a random number is equal to a number I have defined.

BUT

If the the user has this item already I want it to skip the code. This is what I have.

#Calculations to see if attacker will find a rare item
if(rand(1,2) == 1) {

#find how many rows are in the rare_items table
$cresult = mysql_query("SELECT * FROM rare_items", $link);
$num_rows = mysql_num_rows($cresult);

#select a row number between one and the amount of rows in rare_items
$rand_row = rand(1,$num_rows);

#select the row where id matches previously selected number
$ranquery = mysql_query("SELECT * FROM rare_items WHERE id = '$rand_row'", $link) or die(mysql_error());
$ranrow = mysql_fetch_array($ranquery) or die ("query 2: " . mysql_error());

$usrquery = mysql_query("SELECT * FROM purchased WHERE username = '$username'", $link) or die(mysql_error());
while($usrrow = mysql_fetch_array($usrquery))
{
if ($ranrow['item_name'] == $usrrow['item_name'])
{

}
else
{
#Insert the rare item into the attackers inventory
$ccresult = mysql_query("INSERT INTO purchased (username, item_type, resell, item_name, damage, type, equipped) VALUES ('$username', '$ranrow[item_type]', '$ranrow[resell]', '$ranrow[item_name]', '$ranrow[damage]', '$ranrow[type]', '0')", $link);

#if the insert was ok
if ($ccresult == TRUE)
{

#declare string variable which displays what item the attacker will get
$rare_item = 'Also you found a '.$ranrow['item_name'].'. This item has been added to your inventory.';

}
}

But what this does is credit the user the same item for every row matching the query lol I just received 42 spiked bats lol. NOT GOOD. Anyone please help. my site is down because of this.

ironik

2:48 am on May 2, 2005 (gmt 0)

10+ Year Member



You've got the insert query in a loop which triggers each time the user item name is not equal to your rare item name.

You'll have to take it out of the loop and tell the script to insert based on a flag:


$usrquery = mysql_query("SELECT * FROM purchased WHERE username = '$username'", $link) or die(mysql_error());
$hasItem = false;
while($usrrow = mysql_fetch_array($usrquery))
{
if ($ranrow['item_name'] == $usrrow['item_name'])
{
$hasItem = true;
}
}

if (!$hasItem)
{
#Insert the rare item into the attackers inventory
$ccresult = mysql_query("INSERT INTO purchased (username, item_type, resell, item_name, damage, type, equipped) VALUES ('$username', '$ranrow[item_type]', '$ranrow[resell]', '$ranrow[item_name]', '$ranrow[damage]', '$ranrow[type]', '0')", $link);

#if the insert was ok
if ($ccresult == TRUE)
{
#declare string variable which displays what item the attacker will get
$rare_item = 'Also you found a '.$ranrow['item_name'].'. This item has been added to your inventory.';
}
}

The random item query portion of your code could also be a problem. Your row id value might not always match up with the total number of rows in a table (you might delete items, and mysql will keep auto-incrementing new values... assuming it is auto incremented):

What you should do is use $rand_row as an offset in the SQL query:


#Get one row using the $rand_row as offset
$ranquery = mysql_query("SELECT * FROM rare_items LIMIT " . $rand_row . ",1", $link) or die(mysql_error());
$ranrow = mysql_fetch_array($ranquery) or die ("query 2: " . mysql_error());

Hope that makes sense

dkin

6:17 am on May 2, 2005 (gmt 0)

10+ Year Member



works beautifully, thanks again.