Re: OOB problem, super stumped.
| From: | Jim Lucas | Date: | Wed, 30 May 2007 04:01:52 +0000 |
| Subject: | Re: OOB problem, super stumped. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-255779@lists.php.net to get a copy of this message | ||
Brian Seymour wrote:
I am super stumped. This works fine separately but when I put everything together it breaks. I has an authenticate class and a sql class. However I always get the same error. SQL class. <?phpYou didn't call the __construct() method of your parent. The above code, should be like this public function __construct($host,$user,$pass,$dbname = null) { parent::__construct($host,$user,$pass,$dbname); echo "Auth constructed"; } You were forgetting to call to the parent and have it initialize the DB connection. In the second part, the $auth->verifyCreds() call, it didn't create a valid db connection to pass as the second arg to the mysql_query() call. And by not passing the $this->conx as the second arg, you are telling it to "use the most recently opened mysql connection. Hope this clears up why it was failing on the latter mysql_query() calls.class SQL {public $host;public $user;public $pass;public $conx;public $db;public $dbname;public $query;public $result;public $fetchedArray;public $nRows;public function __construct($host,$user,$pass,$dbname = null){$this->host=$host;$this->user=$user;$this->pass=$pass;$this->conx=$this->connection($host,$user,$pass);if (!is_null($dbname)){ $this->selectDb($dbname); }}final public function connection($host,$user,$pass){$this->conx=mysql_connect($host,$user,$pass) ordie(mysql_error());}final public function selectDb($db){$this->db=mysql_select_db($db);}final public function query($query){$this->result=mysql_query($query, $this->conx);echo mysql_error();echo $query;return $this->result;}final public function fetchArray($query){$this->result=$this->query($query);$this->fetchedArray=mysql_fetch_array($this->result,MYSQL_ASSOC);return $this->fetchedArray;}final public function makeArray($query){$this->curArray=mysql_fetch_array($query,MYSQL_ASSOC);return $this->curArray;}final public function numRows($result){$this->nRows=mysql_num_rows($result);return $this->nRows;}public function __destruct(){if (isset($this->connection)){mysql_close($this->connection); }}}?> Authenticate class <?phpclass Authentication extends SQL {public $errorMsg;public function __construct(){echo "Auth constructed";}
-- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Unknownfinal public function verifyCreds ($user, $pass, $table){$result = $this->query("SELECT * FROM $table where$user='$pass'");if ($this->numRows($this->result) == 0){$this->errorMsg = "Incorrect Username/PasswordCombo";return false;}else{// debugging lines \/echo "login good!";// debugging lines /\return true;}}public function __destruct(){}}?> Normal page. <?php/************************************************************* common.php* * project: Renegades Revenge* programmer: Brian Seymour************************************************************/// autoload classesfunction __autoload($class_name) {require_once 'includes/classes/class_' . strtolower($class_name). '.php';}// initialize Renegades Revenge database$database = new SQL($host,$user,$pass,"aerocor_renegade");// loginif (isset($_GET['login'])){$auth = newAuthentication($host,$user,$pass,"aerocor_renegade");if($auth->verifyCreds($_POST['username'],$_POST['password'],"players")){echo "logged in good!";}}?> The form is just 2 fields. Username and password. I get this error. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/aerocor/public_html/rr/includes/classes/class_sql.php on line 52 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/aerocor/public_html/rr/includes/classes/class_sql.php on line 71 I put some simple query and display code in the constructor for the SQL class and it outputted from the database with no problem, that's whats weird. Any help would be great, thanks. Brian