Class problems

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
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Class problems

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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'");
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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';
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

volka, thanks for helping me indirectly....

who woulda thought too many dollars was a bad thing...
$this->$orders
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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 $?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

obviously not ;)
Coco wrote:who woulda thought too many dollars was a bad thing...
seems like dollars have a slump in prices
Post Reply