Arrrgh! (variables..)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Arrrgh! (variables..)

Post 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.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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);
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Arrrgh! (variables..)

Post by RobertGonzalez »

Kieran, that was some good stuff bro... :D

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.");
}
?>
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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. :D

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?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post 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.
Post Reply