无法使用mysql连接到数据库

时间:2013-11-25 03:10:38

标签: php mysql mysqli

我有一个问题需要大家帮助我。我被困了好几天了。我的问题与使用php连接到数据库有关。在phpmyadmin中启用了Mysqli和Mysql。我可以通过Mysqli连接到数据库,但我无法通过Mysql连接。

以下是代码

的mysqli

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
define('PORT', '80');
define('SOCKET', '/var/lib/mysql/mysql.sock');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE, PORT, SOCKET) or die("Error " . mysqli_error($connection));
mysqli_set_charset($connection, "utf8");
?>

Mysql的

<?php
define('DB_SERVER', 'localhost:/var/lib/mysql/mysql.sock');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die("Error " . mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset($connection, "utf8");
?>

你能帮我解决这个问题吗?

提前感谢!

1 个答案:

答案 0 :(得分:-1)

你看到哪种类型的错误? 尽量使用而不是

 define('DB_SERVER', 'localhost');

 define('DB_SERVER', 'ipOfTheHost');

// data db
$username = "username";
$pass = "pwd";
$database = "nameDB";
$host = "ipServer"; //my is 62.149.150.187

// connection
mysql_connect($host, $username, $pass);
@mysql_select_db($database) or die("Argh... it's impossible to connect to it");