Vermillion Posted August 21, 2008 Share Posted August 21, 2008 Okay yeah, I have this piece of code: <?php ################################################################################ # SCRIPT: Register Validator # # SCRIPT NAME: class.registervalidator.php # # AUTHOR: Andrés Ibañez | Vermillion. # ################################################################################ ################################################################################ # This is the main class for a member. However, other classes will be # # heredated from this one because of that, so everything here needs to be on # # protected: To allow the heredited classes to access members and functions, # # and not other classes. # ################################################################################ ################################################################################ # The script that will work with this class, or with the objects of this # # class, should include the "dbdetails.php" file on the includes folder. This # # is because this class will connect to the database. # ################################################################################ class member{ protected $username; protected $password; protected $question; protected $answer; protected $email; protected $fname; protected $lname; protected $byear; protected $bmonth; protected $bday; protected $gender; protected $date; protected $time; protected $bdate; protected $ip; //User-related methods. function setUsername($username){ $this -> username = $username; } function getUsername(){ return $this -> username; } function checkChars($user){ if(preg_match("/[\[\]\^\\\{\}\*\"\'@\?\`\´#\|\=\(\)\+\:\°]/is", $user)){ return 1; } else { return 0; } } function checkExistence($user){ mysql_connect($dbhost,$dbuser,$dbpass); mysql_select_db($dbactual); if(mysql_num_rows(mysql_query("SELECT * FROM members WHERE username = '$user'")) > 0){ return 1; // IF it exists, return 1. } else { return 0; } } // Password-related methods. function valPassword($pass1, $pass2){ $passcount = strlen($pass); $pass1 = md5($pass1); $pass2 = md5($pass2); if($passcount > 6 || $pass1 != $pass){ return 0; } } function setPassword($pass1){ $this -> password = $pass1; } function getPassword(){ return $this -> password; } } ?> If you see the checkExistence() method on it, you will see that it uses some MySQL stuff. So I include the class here: <?php include "vghack/classes/class.registervalidator.php"; include "vghack/includes/dbdetails.php"; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="user" /><input type="password" name="pass1" /><input type="password" name="pass2" /><input type="submit" /> </form> <?php if($_POST){ $user = new member; $user -> setUsername($_POST['user']); echo $user -> getUsername(); echo "<br /><br />"; echo $user -> checkChars($user -> getUsername()); echo "<br /><br /><br />"; echo $user -> valPassword($_POST['pass1'], $_POST['pass2']); echo "<br /><br />"; echo $user -> getPassword(); echo "<br /><br />"; echo $user -> checkExistence($_POST['user']); } ?> To see wether my stuff works or not, but I get a MySQL error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\wamp\www\vghack\classes\class.registervalidator.php on line 73 I think the problem is the class, because I tried the code normally, outside a method, and it worked fine. Any help, please? -- Edit: Just noticed the O.O.P forum... I am not sure if this should be moved there, but if it is needed, I will appreciate it if it just moved and not closed. Quote Link to comment https://forums.phpfreaks.com/topic/120780-solved-php-classes-and-mysql/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 21, 2008 Share Posted August 21, 2008 The mysql function calls in that class have no error checking, error reporting, or error recovery logic. The error means that the mysql_query failed and returned a FALSE value instead of a result resource. There are several possible reasons - a failed connection to a mysql database server, a failed selection of a database, or a syntax error in the query (such as a table or column name mis-match.) Quote Link to comment https://forums.phpfreaks.com/topic/120780-solved-php-classes-and-mysql/#findComment-622567 Share on other sites More sharing options...
Vermillion Posted August 22, 2008 Author Share Posted August 22, 2008 Thanks for the reply, although, I'm still having problems. When I just put the exact following MySQL codes outside of classes, it works, but now I am even trying on my class file itself, including the database details and everything, and nothing happens: <?php ################################################################################ # SCRIPT: Register Validator # # SCRIPT NAME: class.registervalidator.php # # AUTHOR: Andrés Ibañez | Vermillion. # ################################################################################ ################################################################################ # This is the main class for a member. However, other classes will be # # heredated from this one because of that, so everything here needs to be on # # protected: To allow the heredited classes to access members and functions, # # and not other classes. # ################################################################################ ################################################################################ # The script that will work with this class, or with the objects of this # # class, should include the "dbdetails.php" file on the includes folder. This # # is because this class will connect to the database. # ################################################################################ include "../includes/dbdetails.php"; class member{ protected $username; protected $password; protected $question; protected $answer; protected $email; protected $fname; protected $lname; protected $byear; protected $bmonth; protected $bday; protected $gender; protected $date; protected $time; protected $bdate; protected $ip; //User-related methods. function setUsername($username){ $this -> username = $username; } function getUsername(){ return $this -> username; } function checkChars($user){ if(preg_match("/[\[\]\^\\\{\}\*\"\'@\?\`\´#\|\=\(\)\+\:\°]/is", $user)){ return 1; } else { return 0; } } function checkExistence($username){ mysql_connect($dbhost,$dbuser,$dbpass); mysql_select_db($dbactual); if(mysql_num_rows(mysql_query("SELECT * FROM members WHERE username = '$username'")) > 0){ return 1; // IF it exists, return 1. } else { return 0; } } // Password-related methods. function valPassword($pass1, $pass2){ $passcount = strlen($pass); $pass1 = md5($pass1); $pass2 = md5($pass2); if($passcount < 6 || $pass1 != $pass){ return 0; } } function setPassword($pass1){ $this -> password = $pass1; } function getPassword(){ return $this -> password; } } $test = new member; $test -> setUsername("Vermillion"); echo $test -> getUsername(); $test -> checkExistence("Vermillion"); ?> Could there be something else causing the problem? Because if I have the following file: <?php class member{ protected $username; function setname($set){ $this -> username = $set; } function getname(){ return $this -> username; } } mysql_connect("localhost","root",""); mysql_select_db("vghack"); $user = new member; $user -> setname("Vermillion"); if(mysql_num_rows(mysql_query("SELECT * FROM members WHERE username = '".$user -> getname()."'")) > 0){ echo "Exists!"; } else { echo "Doesn't exist!"; } ?> Which is basically the same thing, except the MySQL functions are not in the class itself. Quote Link to comment https://forums.phpfreaks.com/topic/120780-solved-php-classes-and-mysql/#findComment-622690 Share on other sites More sharing options...
Vermillion Posted August 22, 2008 Author Share Posted August 22, 2008 Never mind, I solved the issue by moving both mysql_connect(), mysql_select_db() to the top of class itself, instead of inside it. It is still inside the class file, but it works now. On another note, sorry for the double post, but the Modify link just dissapeared... Quote Link to comment https://forums.phpfreaks.com/topic/120780-solved-php-classes-and-mysql/#findComment-622703 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.