skkeeper Posted December 25, 2007 Share Posted December 25, 2007 i'm just starting in php and oop, but im writting a little class to use in all my mysql querys. Unfortanelly im not very sucessfull until now, here is why My class class mysql_connection { var $server = 'localhost'; var $username = 'root'; var $password = ''; var $database = 'test'; function execute($query) { //Open Connection $link = mysql_connect($server,$username,$password); mysql_select_db($database); //Execute query echo(mysql_error($link).mysql_errno($link)); $data = mysql_query($query,$link); //Close connection mysql_close($link); } } My test file include('oop_mysql.php'); $db = new mysql_connection; $db->execute("INSERT INTO test_table VALUES('teste','teste2')"); Im getting the following error Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\oop_mysql.php on line 18 Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\oop_mysql.php on line 20 Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\wamp\www\oop_mysql.php on line 20 Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 23 Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 23 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 24 Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\oop_mysql.php on line 27 I'm completely stuck, im using wamp with all default configs i cant figure it out why is using the name ODBC instead of root just like i declare regards and merry xmas Quote Link to comment https://forums.phpfreaks.com/topic/83170-solved-oop-mysql-problem/ Share on other sites More sharing options...
trq Posted December 25, 2007 Share Posted December 25, 2007 You might want to take alook at how properties are accessed within classes in the oop section of the manual. Also, your class has no error handling. I would set it up more like (assuming your using php4).... <?php class mysql_connection { var $server = 'localhost'; var $username = 'root'; var $password = ''; var $database = 'test'; var $link; var $err; function mysql_connection() { $link = mysql_connect($this->server,$this->username,$this->password); mysql_select_db($this->database); } function execute($query) { if ($data = mysql_query($query,$link)) { return $data; } else { $this->err = mysql_error(); } } function geterr() { if ($this->err) { return $this->err; } } } Quote Link to comment https://forums.phpfreaks.com/topic/83170-solved-oop-mysql-problem/#findComment-423059 Share on other sites More sharing options...
skkeeper Posted December 25, 2007 Author Share Posted December 25, 2007 thank you that basically solved it, and thx for the tips about error handling. btw: sry for posting in the wrong section, i didnt noticed there was a section of help just for oop. Quote Link to comment https://forums.phpfreaks.com/topic/83170-solved-oop-mysql-problem/#findComment-423064 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.