Page 1 of 1
Arrrgh! (variables..)
Posted: Mon Feb 19, 2007 10:49 am
by Mightywayne
Omg. It's like. Sometimes it works, sometimes it doesn't. Well this time it's not working for a lot of stuff. Look here.
Code: Select all
$monnum = mysql_query("SELECT monnum FROM user WHERE username = '$user'");
$monnum = mysql_fetch_array($monnum);
// Monster checkage.
if ($monnum == 5)
{
die("I'm afraid you have too many monsters... you'll have to get rid of one before the city lets you adopt another.");
}
No matter what their monnum is, it will die and say they have too many monsters. I even changed the == to = and still nothing. >_>; What's going on? I have another error like this and I figure there's just one thing I'm forgetting.
... also, if I try {$monnum['monnum']} == 5, it tells me there's a syntax error.
Posted: Mon Feb 19, 2007 11:02 am
by blackbeard
Try this:
Code: Select all
$query = mysql_query("SELECT monnum FROM user WHERE username = '$user'");
$row = mysql_fetch_assoc($query);
$monnum = $row['monnum'];
if ($monnum >= 5)
// code
else
// other code
Posted: Mon Feb 19, 2007 11:19 am
by Kieran Huggins
For some reason, your post made me think:
Code: Select all
SELECT `3117 d00d` FROM `rofl` WHERE `I'm all` LIKE 'OMG, WTF! BBQ!!!1!111!!'
Now for being good natured about that gentle ribbing, I offer the following:
Code: Select all
$result = mysql_query("SELECT monnum FROM user WHERE username = '$user'") or die('ACK! '.MySQL_error());
while($row = MySQL_fetch_assoc($result)){
print_r($row);
}
Re: Arrrgh! (variables..)
Posted: Mon Feb 19, 2007 11:32 am
by RobertGonzalez
Kieran, that was some good stuff bro...
Code: Select all
<?php
// Run a query, get a result resource
$monnum = mysql_query("SELECT monnum FROM user WHERE username = '$user'");
// read the resource into an array
$monnum = mysql_fetch_array($monnum);
// At this point $monnum will be array(). Since array() never equals 5, this always returns false.
if ($monnum == 5)
{
die("I'm afraid you have too many monsters... you'll have to get rid of one before the city lets you adopt another.");
}
?>
Posted: Mon Feb 19, 2007 11:52 am
by Mightywayne
LOL that was good right there. Yeah, I'm making a game.
Worked perfectly using blackbeard's code. I'll try it on my other stuff and hopefully I can make the actual monster page today.
Edit: ACTUALLY I do have a question, look here. This code works fine.
Code: Select all
$sql = mysql_query("SELECT temp FROM user WHERE username = '$user'");
$type = mysql_fetch_array($sql);
It inserts the type fine when it's called upon. Isn't this the same instance, though?
Posted: Mon Feb 19, 2007 3:05 pm
by Mightywayne
I'm actually getting sick of asking for help. I've spent an hour+ fixing this damn script around, and finally the only thing wrong with it is the following code.
Code: Select all
$name = $_POST['monname'];
$sql = mysql_query("SELECT temp FROM user WHERE username = '$user'");
$type = mysql_fetch_array($sql);
//$monnum = mysql_query("");
// $monnum = mysql_fetch_array($monnum);
$query = mysql_query("SELECT monnum FROM user WHERE username = '$user'");
$row = mysql_fetch_array($query);
$monnum = $row['monnum'];
mysql_query("INSERT INTO monster (ID, name, type, traintype, lifeexp, wins, losses, srp, stam, str, con, agi, luk, age, item1, item2, trainer)
VALUES ('', '$name', '{$type['temp']}', '', '90', '0', '0', '0', '1', '3', '3', '3', '3', '0', '', '', '$user')")or die(mysql_error());
$query = mysql_query("SELECT ID FROM monster WHERE trainer = '$user' AND name = '$name'");
$row = mysql_fetch_array($query);
$idtoadd = $row['ID'];
if ($monnum == '0')
{
mysql_query("UPDATE user SET monid1 = '$idtoadd' WHERE username = '$user'");
}
if ($monnum == '1')
{
mysql_query("UPDATE user SET monid2 = '$idtoadd' WHERE username = '$user'");
}
if ($monnum == '2')
{
mysql_query("UPDATE user SET monid3 = '$idtoadd' WHERE username = '$user'");
}
if ($monnum == '3')
{
mysql_query("UPDATE user SET monid4 = '$idtoadd' WHERE username = '$user'");
}
if ($monnum == '4')
{
mysql_query("UPDATE user SET monid5 = '$idtoadd' WHERE username = '$user'");
}
It eventually adds monnum to 127 and I guess it gives up after that. Don't ask me why, but, it just does. So anyway. I've taken away a piece of the code that I thought was doing it, and STILL nothing. I see NOTHING that would add anything to monnum.
Edit: Is there a way to donate to Devnetwork? I feel I atleast owe something to you guys. I'd still be making cookies if not for ya'll.
Posted: Mon Feb 19, 2007 5:25 pm
by RobertGonzalez
For the sake of debugging, can you please throw a ' or
die(mysql_error());' after your calls to
mysql_query(). It might tell you a lot about what is happening. If not, we will review the code to look for other potential problems.
Posted: Tue Feb 20, 2007 9:43 am
by Mightywayne
I just added them, and nothing. I don't think the script realizes a problem is happening. I'll look more at the actual code and see if I can find a correct function for this.
Edit: ... okay ... I don't know what happened. But now it randomly works, and perfectly... Sorry for the trouble.