Page 1 of 1

Class problems

Posted: Sun Oct 13, 2002 9:52 am
by Coco
k, this is my first time playing with classes and functions, so if its a dumb problem then sorry...
here is my definition:

Code: Select all

<?php
class play
{
	var $user;
	var $orders;

	function fill()
	{
		$resulta = mysql_query("SELECT or1, -lost of stuff- or40 FROM Players WHERE user = '$user'");
		$this->$orders = mysql_fetch_row($resulta);
		echo "<br> $user <br>";
		print_r($orders);
	}
}
?>
and i use this for new objects:

Code: Select all

<?php
for($k=0;$k<$i;$k++){
	$temp = 'player' . $k;
	$$temp = new play;
	$$temp->user = $usersї$k];
	$$temp->fill();
	}
?>
I know the for loop runs fine, i tested it. But the function in my class doesnt. (the echos dont run and $$temp->orders is empty.

Any ideas what im doing wrong?

Posted: Sun Oct 13, 2002 10:06 am
by volka
php class-methods do not have a 'this-autoscope' on variables.
Try

Code: Select all

$resulta = mysql_query("SELECT or1, -lost of stuff- or40 FROM Players WHERE user = '$this->user'");

Posted: Sun Oct 13, 2002 10:27 am
by Coco
ok my bad that was silly...
thing is now that i have put this-> infront of all the variables inside the function, the sql isnt working :(
i know the connection is live and good cos i use it just before i call the class... am i doing something glaringly wrong?

Posted: Sun Oct 13, 2002 12:16 pm
by twigletmac
Try this:

Code: Select all

$sql = "SELECT or1, -lost of stuff- or40 FROM Players WHERE user = '".$this->user."'";
$resulta = mysql_query($sql);
or this

Code: Select all

$sql = "SELECT or1, -lost of stuff- or40 FROM Players WHERE user = '{$this->user}'";
$resulta = mysql_query($sql);
Mac

Posted: Sun Oct 13, 2002 12:19 pm
by volka
I'm quite sure that php will replace ".. $this->user ..." correct but (and now comes the usual database-answer :D ) try

Code: Select all

echo '<!-- ', $sql, ' -->';
$resulta = mysql_query($sql) or die($sql.' :'.mysql_error());
in case twigletmac didn't solve your problem

Posted: Sun Oct 13, 2002 12:30 pm
by twigletmac
Sorry it's heredoc format that doesn't like dem $this->'s. But then it was only a suggestion. Of course mysql_error() is your best friend for when good queries go bad (but getting the SQL statement out of the mysql_query() call and into it's own variable is the first step :wink: 'cause otherwise you can't echo it out to check on what it thinks it looks like).

Mac

Posted: Sun Oct 13, 2002 2:11 pm
by Coco
ok i tried both what you said and it didnt work...
ive got the query exactly as it should be:

Code: Select all

<?php
		$sql = "SELECT or1 -lots of stuff- or40 FROM Players WHERE owner = '" . $this->user . "'";
		echo $sql;
		$this->resulta = mysql_query($sql);
		if(mysql_error())
			echo mysql_error();
		$this->$orders = mysql_fetch_row($this->resulta);
		echo "<br> $this->user <br>";
		print_r($this->orders);
?>
but it either isnt printing the array or the array doesnt exist...
so, does print_r have problems with objects? cos from what i can tell thats what is causing the trouble

Posted: Sun Oct 13, 2002 2:26 pm
by volka
maybe it's as simple as there's nothing in the result-set

Code: Select all

$this->$orders = mysql_fetch_row($this->resulta) or $this->$orders = 'no results';

Posted: Sun Oct 13, 2002 5:31 pm
by Coco
volka, thanks for helping me indirectly....

who woulda thought too many dollars was a bad thing...
$this->$orders

Posted: Sun Oct 13, 2002 7:02 pm
by hob_goblin
Just something i noticed but,

you're doing

Code: Select all

print_r($this->orders);
but, you declare it by

Code: Select all

$this->$orders
did you mean to have the $?

Posted: Sun Oct 13, 2002 8:17 pm
by volka
obviously not ;)
Coco wrote:who woulda thought too many dollars was a bad thing...
seems like dollars have a slump in prices