Jump to content

Recommended Posts

Hello,

 

I've been working on a new website (php/mysql) and have run into a snag now that I've started trying to implement classes.

 

I have two classes that I'm trying to get working to start things out, a Database class and a UserData class.

 

The Database class is pretty straightforward:

 

<?php 
class Database 
{
private $host;
private $user;
private $password;
private $databasename;
private $database;	//result of mysql_connect()

//constructor
public function __construct($host, $usr, $pw, $db) {
	$this->host = $host;
	$this->user = $usr;
	$this->password = $pw;
	$this->databasename = $db;
}

//methods
private function connect() {
	$this->database = mysql_connect($this->host, $this->user, $this->password) 
						or die(mysql_error());
	mysql_select_db($this->databasename, $this->database) or die(mysql_error());							
}

public function query($sql) { 
	$this->connect();
	$result = mysql_query($sql) or die(mysql_error());
	mysql_close($this->database);
	return $result;
}
}
?>

 

Nothing fancy there, query() expects it's input to be verified, but other than that, nothing that seems to me like it would cause a 500 server error.

 

So the other class is defined like this:

 

<?php 
require("database.php");
class UserData
{
//data members
private $userdataid; //database userdata table primary key - uint

private $firstname;	//25 char limit
private $middlename;    //25 char limit
private $lastname;	//30 char limit
private $gender;	        //20 char limit
private $race;		//50 char limit
private $avatar;	        //30 char limit
private $birthdate;	//mysql date format
private $age;		//tinyint(3) unsigned	
private $location;   	//100 char limit
private $occupation;    //100 char limit
private $hobbies;  	//text blob
private $biography;	//text blob
private $postheader;    //200 char limit - a header to attach to all the user's posts
private $postfooter;     //200 char limit - a footer to attach to all the user's posts
private $commentsig;   //250 char limit - a signature to attach to the footer of all user's comments	
private $website;	       //100 char limit
private $email;	       //150 char limit

public function __construct() {
	//construct a default one for a new user
	$this->applyDefaults();
	$database = new Database("","","",""); //arguments withheld to protect my database
	$sqlquery = "INSERT INTO userdata "
			.	"VALUES('-1', "
			.	"'$this->firstname','$this->middlename', "
			.	"'$this->lastname, '$this->gender', "
			.	"'$this->race', '$this->avatar', "
			.	"'$this->birthdate', '$this->age', "
			.	"'$this->location', '$this->occupation', "
			.	"'$this->hobbies', '$this->hobbies', "
			.	"'$this->biography', '$this->postheader', "
			.	"'$this->postfooter', '$this->commentsig', "
			.	"'$this->website', '$this->email');"; 
	$result = $database->query($sqlquery);
	if(mysql_affected_rows($result) != 1)
		die("Database Error - insertion of new user data failed.");							
}

private function applyDefaults() {
	//sets all the data back to default in this object
	//*NOTE - does not commit to database
	$this->firstname 	        = "unknown";
	$this->middlename 	= "";
	$this->lastname 	        = "unknown";
	$this->gender 		= "";
	$this->race 		= "";
	$this->avatar 		= "unknown.png";
	$this->birthdate 	        = "";
	$this->age 			= "";
	$this->location 	        = "Planet Earth"
	$this->occupation 	= "";
	$this->hobbies 		= "";
	$this->biography 	        = "";
	$this->postheader 	= "";
	$this->postfooter 	= "";
	$this->commentsig 	= "";
	$this->website 		= "http://";
	$this->email 		= "";		
}

public function commitToDatabase() {
	if(is_integer($this->userdataid) and ($this->userdataid >= 0)) {
		//commits all data to the database if the userdataid is valid
		$database = new Database("","","",""); //arguments withheld to protect my database
		$sqlquery = "UPDATE userdata "
				. 	"SET firstname = '$this->firstname', "
				.	"middlename = '$this->middlename', "
				.	"lastname = '$this->lastname', "
				. 	"gender = '$this->gender', "
				.	"race = '$this->race', "
				.	"avatar = '$this->avatar', "
				.	"birthdate = '$this->birthdate', "
				.	"age = '$this->age', "
				. 	"location = '$this->location', "
				.	"occupation = '$this->occupation', "
				.	"hobbies = '$this->hobbies', " 
				.	"biography = '$this->biography', "
				.	"postheader = '$this->postheader', "
				.	"postfooter = '$this->postfooter', "
				.	"commentsig = '$this->commentsig', "
				.	"website = '$this->website', "
				.	"email = '$this->email' "
				.	"WHERE userdataid = '$this->userdataid';";
		$result = $database->query($sqlquery);
		if(mysql_affected_rows($result) != 1) {
			$error = sprintf("Database Error - attempted to update non-existant row. - data id - %u - ", $this->userdataid);
			die($error);
		}													
	}
}

public function __toString() {
	$format = "<br />%u | %s %s %s <br /> "
			.	"%s - %s - %s <br /> "
			.	"%s - %u <br /> "
			.	"%s <br /> %s <br /> %s <br /> %s <br /> "
			.	"%s <br /> %s <br /> %s <br /> %s <br /> "
			.	"%s <br /> %s <br /><br />";
	$echo_out = sprintf($format,
						$this->userdataid, $this->firstname, $this->middlename, $this->lastname, 
						$this->gender, $this->race, $this->avatar, 
						$this->birthdate, $this->age, 
						$this->location, $this->occupation, $this->hobbies, $this->biography,
						$this->postheader, $this->postfooter, $this->commentsig, 
						$this->website, $this->email);
	return $echo_out;
}
}

 

There is a little more, I had another __construct function with an id argument and a method to construct a UserData object from the database, given a valid id, but I tried removing that constructor and just using the one I have here and that didn't make any difference.

 

So the Database class is in the Database.php file, and the UserData class is in the user.php file.  User.php require()'s the database file right away, then defines the class and its methods, and then I wrote a tiny little script at the end of the file to see if everything would work:

 

<?php 
$testdata = new UserData();
echo "<html><head><title>test</title></head><body><div>";
echo $testdata;
echo "</div></body></html>";
?>

 

When I upload both files to my site (php version 5.2.6 installed) to try them out, and go to user.php, I get a 500 server error.

 

I'm stumped here, because while I know the syntax for OOP in C++ very well, OOP in PHP is new to me.  I don't know if I'm making a syntax error, or some other silly mistake, and I'm not really sure how to debug it any further.  Are there any good tools available for debugging PHP scripts?

 

Anyone have any suggestions as to what I might be missing here?

 

Thanks in advance for any help!

 

~~~Tantalus

Finally I'm getting somewhere...

 

It dawned on me that I could check out my script errors by going to www/logs/script.error_log  ::)

 

So I cleared it out and started running my script again, and I started getting better details about just where I hosed up the syntax.

 

Thanks genericnumber1, I did catch that one just a minute ago... silly semicolons.

 

Now all I'm stuck with is a bad sql query somewhere in there... but at least I'm making progress!  Thanks!

You should be learning php (or learning something new in php), developing php code, or debugging php code on a local development system where error_reporting is set to E_ALL and display_errors is set to ON in your php.ini so that all php generated errors are displayed. This will result in the quickest learning, development, and debugging cycle.

      $this->location            = "Planet Earth"

 

You forgot a semicolon after this line, but you shouldn't be getting an internal server error for a little missed semicolon...

 

 

When ever show errors is off, sometimes Apache just spits out a 500 error on fatal parse errors.  Weird, eh?  But kinda makes sense since it's a parse error.

I've never actually run into that problem, I always get php errors for php problems (like syntax issues) and internal server errors for internal server problems (like a typo in an htaccess file)... guess I'm just lucky. I'll keep that in mind, thanks.

      $this->location            = "Planet Earth"

 

You forgot a semicolon after this line, but you shouldn't be getting an internal server error for a little missed semicolon...

 

 

When ever show errors is off, sometimes Apache just spits out a 500 error on fatal parse errors.  Weird, eh?  But kinda makes sense since it's a parse error.

 

It's not weird. It'll happen if you are running PHP using CGI.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.