Tantalus Posted November 19, 2008 Share Posted November 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/ Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 $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... Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-693242 Share on other sites More sharing options...
Tantalus Posted November 19, 2008 Author Share Posted November 19, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-693251 Share on other sites More sharing options...
PFMaBiSmAd Posted November 19, 2008 Share Posted November 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-693366 Share on other sites More sharing options...
corbin Posted November 20, 2008 Share Posted November 20, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-694038 Share on other sites More sharing options...
genericnumber1 Posted November 20, 2008 Share Posted November 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-694110 Share on other sites More sharing options...
Daniel0 Posted November 20, 2008 Share Posted November 20, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-694688 Share on other sites More sharing options...
corbin Posted November 20, 2008 Share Posted November 20, 2008 I meant weird as in requiring thought. Not weird as in illogical or strange. Guess I should've chosen a different word ;p. Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-694777 Share on other sites More sharing options...
Mark Baker Posted November 21, 2008 Share Posted November 21, 2008 The command line is your friend php -l <filename> Quote Link to comment https://forums.phpfreaks.com/topic/133292-class-definition-causes-500-internal-server-error/#findComment-695258 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.