fantomel Posted October 17, 2008 Share Posted October 17, 2008 hello i'm trying to build a mysql class from scratch first a primitive one then i will improve it <?php class MySQL { var $host; var $name; var $pass; var $dba; private $connection; public function __construct($host, $name, $pass, $dba) { if($host && $name && $pass & $dba) { if(!$this->connection = mysql_connect($host,$name,$pass)) throw new MySQL_error("Database Connection Failed!"); if(!mysql_select_db($dba, $this->connection)) throw new MySQL_error("Database Can't Be Selected"); } else { throw new Inter_error("Method: __construct--Error:Incorect arg(requires \$host, \$name, \$pass, \$dba)!!!!"); } } public function Close() { mysql_close($this->connection); } public function query() { return mysql_query($this->connection); } public function fetch_assoc(){ return mysql_fetch_assoc($this->connection); } } ?> that is my code for the moment.. and i wanted to test it a little bit see how it works .. stuff.. for the first three functions but i run out on this error Warning: mysql_fetch_assoc(): supplied resource is not a valid MySQL result resource in C:\webserver\sandbox.dev\public_html\classes\mysql.class.php on line 46 Please can someone help me to solve the problem.. posibly give me some advices ? thank you Quote Link to comment Share on other sites More sharing options...
Archadian Posted October 17, 2008 Share Posted October 17, 2008 mysql_query and mysql_fetch_assoc...I could be wrong (highly doubt it) but these two functions pull data from the actual database. Why in God's name you are passing $this->connection in them is beyond me. Maybe reading up on the functions would help a little. mysql_query: http://us2.php.net/mysql_query mysql_fetch_assoc: http://us2.php.net/mysql_fetch_assoc Quote Link to comment Share on other sites More sharing options...
Jibberish Posted October 17, 2008 Share Posted October 17, 2008 <?php class MySQL { var $host; var $name; var $pass; var $dba; var $resultLink; private $connection; public function __construct($host, $name, $pass, $dba) { if($host && $name && $pass & $dba) { if(!$this->connection = mysql_connect($host,$name,$pass)) throw new MySQL_error("Database Connection Failed!"); if(!mysql_select_db($dba, $this->connection)) throw new MySQL_error("Database Can't Be Selected"); } else { throw new Inter_error("Method: __construct--Error:Incorect arg(requires \$host, \$name, \$pass, \$dba)!!!!"); } } public function Close() { mysql_close($this->connection); } public function query($query) { $this->resultLink = mysql_query($query, $this->connection); } public function fetch_assoc(){ return mysql_fetch_assoc($this->resultLink); } } ?> Quote Link to comment Share on other sites More sharing options...
fantomel Posted October 17, 2008 Author Share Posted October 17, 2008 sorry for mistaking there.. i didn't know exacly how what.. should i do thaks for the help now i know Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted October 17, 2008 Share Posted October 17, 2008 hey sorry don't mean to discourage you but there's already a "mysql" class (and a "mysqli" class and a "PDO" class for that matter!) definitely a good learning exercise to write a DB-interaction class but i'd read up on what's already available (maybe you just need to extend one of the existing classes?) and name it something different Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted October 17, 2008 Share Posted October 17, 2008 attached is a small script that tests SQL statements... its definitely not pretty and not optimized but its gets the job done which is: send a query and see the results. I've found it useful to debug sql-related errors by echoing my $sql variable and then copy and pasting it into the script to see what happens... easier than var_dumping everything im kind of tied up at work but if the OP is still having problems i'll be happy to check back and help later! -Alex [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
fantomel Posted October 17, 2008 Author Share Posted October 17, 2008 attached is a small script that tests SQL statements... its definitely not pretty and not optimized but its gets the job done which is: send a query and see the results. I've found it useful to debug sql-related errors by echoing my $sql variable and then copy and pasting it into the script to see what happens... easier than var_dumping everything im kind of tied up at work but if the OP is still having problems i'll be happy to check back and help later! -Alex very nice script and yes it's very useful for debuging Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted October 21, 2008 Share Posted October 21, 2008 fantomel, your code has changed so much from PMs to the thread that I'm lost! Did you fix your problem? And if not please post the entire class and test code that generates the error and of course the error itself! I realize that there might be some hesitance in posting an entire class code here or anywhere for that matter (unless releasing under an open-source license) so if you must omit parts of your code try to omit the ones that aren't relevant to the problem at hand please! Anyway, waiting on a reply! Alex Quote Link to comment 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.