-2

I have MySQL server running on my local computer. I am trying to connect to it through PHP from my website host. I get the following error:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).

Here is the php I am running with the userid and password wiped out for security:

 if ($dbc = @mysql_connect('localhost', 'userid', 'password')) {

print '<p>Successfully connected to MySQL!</p>';

// Try to create the database:
if (@mysql_query('CREATE DATABASE myblog',$dbc)) {

    print '<p>The database has been created!</p>';

} else { // Could not create it.
    print '<p style="color: red;">Could not create the database because:<br />' . mysql_error() . '.</p>';
}

// Try to select the database:
if ($x == @mysql_select_db('myblog,$dbc')) {
    print '<p>The database has been selected.</p>';
} else {
    print '<p style="color: red;">Could not select the database because:<br />' . mysql_error() . '.</p>';
}

mysql_close(); // Close the connection.

 } else {

print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>';

Can someone help me with this connection?

rickzipser
  • 135
  • 1
  • 1
  • 7

1 Answers1

1

It seems that your code should work unless your MySQL server is not properly configured... but you may try the MySQLi Route.

$servername = "computername";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

?>