Can';t使用PHP连接到MySQL服务器
我在本地计算机上运行MySQL服务器。我正在尝试连接到它 通过PHP从我的网站主机。我得到以下错误: 无法通过套接字“/var/run/mysqld/mysqld.sock”(2)连接到本地MySQL服务器 以下是我运行的php,为了安全起见,删除了用户名和密码:Can';t使用PHP连接到MySQL服务器,php,mysql,connect,Php,Mysql,Connect,我在本地计算机上运行MySQL服务器。我正在尝试连接到它 通过PHP从我的网站主机。我得到以下错误: 无法通过套接字“/var/run/mysqld/mysqld.sock”(2)连接到本地MySQL服务器 以下是我运行的php,为了安全起见,删除了用户名和密码: if ($dbc = @mysql_connect('localhost', 'userid', 'password')) { print '<p>Successfully connected to MySQL!<
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>';
if($dbc=@mysql\u connect('localhost','userid','password')){
打印“成功连接到MySQL!”;
//尝试创建数据库:
if(@mysql_query('CREATE DATABASE myblog',$dbc)){
打印“数据库已创建!”;
}否则{//无法创建它。
无法创建数据库,因为:
。mysql_error()。;
}
//尝试选择数据库:
如果($x==@mysql\u select\u db('myblog,$dbc')){
打印“已选择数据库。”;
}否则{
打印'无法选择数据库,因为:
'.mysql_error();
}
mysql_close();//关闭连接。
}否则{
打印“
无法连接到MySQL:
”。MySQL_错误();
有人能帮我建立这个连接吗?除非您的MySQL服务器配置不正确,否则您的代码应该可以工作……但是您可以尝试MySQLi路由。
$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);
}
?>
可能重复:看起来您的mysql服务器没有运行