Ok so I am trying to create a class that connects to mysql and runs a query. Here is the code:
[code=php]
class dbConnect {
public $host;
public $user;
public $passwd;
public $dataB;
function __construct($host,$user,$passwd,$dataB) {
$this->host = $host;
$this->user = $user;
$this->passwd = $passwd;
$this->dataB = $dataB;
}
function connect($sql) {
$conn=mysql_connect($host,$user,$passwd) or die (mysql_error());
$db=mysql_select_db($dataB,$conn) or die (“Unable to connect to database1”);
$result = mysql_query($sql, $conn) or die (“Couldn’t Connect”);
return $result;
}
}
$connect1 = new dbConnect(“127.0.0.1″,”example_user”,”example_pass”,”database”);
$db_query = $connect1->connect(“SELECT * FROM database”);
When I run this, I get the following error:
“Warning: mysql_connect() [function.mysql-connect]: Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2)”. I know that I can connect, I have done so hundreds of times, just not using the above code. I verified that the data is being passed into the class with the following code:
[code=php]print “host: {$connect1->host}<br>”;
print “user: {$connect1->user}<br>”;
print “password: {$connect1->passwd}<br>”;
print “database: {$connect1->dataB}”;
This displays all the information as passed into the class. Any ideas why its not working?