sneamia Posted September 26, 2007 Share Posted September 26, 2007 I'm just starting off with OOP, and to be honest, no tutorials provide some applicable examples, so I decided to learn through trial and error. I'm writing a basic MySQL class, and I'm running into this error. Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2) in /homepages/.../classes/mysql.php5 on line 10 index.php5: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php error_reporting(E_ALL); require_once('classes/mysql.php5'); $result = new mysql(); unset($result); ?> </body> </html> mysql.php5: <?php class mysql { protected $hostname = 'xxxxxx.perfora.net'; protected $username = 'dboxxxxxxxx'; protected $password = 'xxxxxxxxx'; protected $dbname = 'dbxxxxxxx'; protected $mysqlconn; protected $querystr; function __construct($hostname = NULL, $username = NULL, $password = NULL, $dbname = NULL) { $mysqlconn = mysql_connect($hostname, $username, $password) or die('Could not connect: ' . mysql_error()); mysql_select_db($dbname, $mysqlconn); } function query($querystr) { } function __destruct() { mysql_close($mysqlconn); } } ?> Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/70694-solved-connecting-to-mysql/ Share on other sites More sharing options...
trq Posted September 26, 2007 Share Posted September 26, 2007 You should probably start with some tutorials or something. <?php class mysql { private $hostname = 'xxxxxx.perfora.net'; private $username = 'dboxxxxxxxx'; private $password = 'xxxxxxxxx'; private $dbname = 'dbxxxxxxx'; private $mysqlconn; private $querystr; function __construct() { $this->mysqlconn = mysql_connect($this->hostname, $this->username, $this->password) or die('Could not connect: ' . mysql_error()); mysql_select_db($this->dbname, $this->mysqlconn); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70694-solved-connecting-to-mysql/#findComment-355459 Share on other sites More sharing options...
sneamia Posted September 26, 2007 Author Share Posted September 26, 2007 Thanks. This works: <?php class mysql { protected $mysqlconn; function __construct($db_hostname = '******', $db_username = '******', $db_password = '******', $db_name = '*****') { $this -> mysqlconn = @mysql_connect($db_hostname, $db_username, $db_password) or die('Could not connect: ' . mysql_error()); mysql_select_db($db_name, $this -> mysqlconn); } function query($querystr) { } function __destruct() { @mysql_close($this -> mysqlconn); } } ?> edit by redbullmarky: be careful with leaving the login info in your code Quote Link to comment https://forums.phpfreaks.com/topic/70694-solved-connecting-to-mysql/#findComment-356003 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.