Windows 10 XAMPP-PDO连接正常,无法通过mysqli或mysqli_connect连接

时间:2019-03-02 11:57:10

标签: php mysql wordpress

我正在使用装有XAMPP(v3.2.2)的Windows 10 PC。

如果我在MySQL中这样做:

select version();
-- 10.1.32-MariaDB

确认版本...

我有一个名为“ wp”的测试数据库,并且用户名和密码都设置为“ wp”。

我可以使用PDO毫无问题地连接到数据库,但是我无法使用mysqli或mysqli_connect进行连接,而且我也看不到为什么。到目前为止,我在此上已经浪费了几个小时。我正在尝试在XAMPP上安装Wordpress,但安装程序说它无法连接到数据库,因此我一直在对此进行测试,因此出现了这个问题。

mysqli

$servername = "localhost";
$username = "wp";
$password = "wp";
$dbname = "wp";
$db = new mysqli($servername, $username, $password, $dbname);

// Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'wp'@'localhost' (using password: YES) in C:\xampp\public_html\_conn.php on line 6

mysqli_connect

$host = "localhost";
$uname = "wp";
$pwd = "wp";
$dbname = "wp";

$conn = mysqli_connect($host, $uname, $pwd, $dbname);

// Warning: mysqli_connect(): (HY000/1045): Access denied for user 'wp'@'localhost' (using password: YES) in C:\xampp\public_html\_conn.php on line 13

pdo

$config['db'] = array(
    'host'      => 'localhost',
    'username'  => 'wp',
    'password'  => 'wp',
    'dbname'    => 'wp'
);

try {
$pdo = new PDO('mysql:host=' .$config['db']['host']. ';port=33066;dbname=' .$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::NULL_EMPTY_STRING, true);
$pdo->exec("SET CHARACTER SET utf8mb4");
}

catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}

// connects fine

wp用户具有正确的访问级别:

enter image description here

由于使用了相同的用户详细信息通过PDO进行连接,但是它们不适用于其他方法。

我已经使用phpinfo();进行了检查,并且可以看到mysqli的设置已经完成(我搜索了mysqli_connect,但没有列出它):

mysqli phpinfo

enter image description here

我缺少明显的东西吗?

1 个答案:

答案 0 :(得分:0)

尝试一下

//This line of code connects to mysql database
define("HOST_NAME", "localhost:3306");
define("HOST_USER", "wp");
define("HOST_PASS", "wp");
define("HOST_DB", "wp");

$db = new mysqli(HOST_NAME, HOST_USER, HOST_PASS, HOST_DB);


 // This line of code checks if connection error exists.

if($db->connect_error) {
    echo $db->connect_errno . " " . $db->connect_error;
} else {
    echo "Connection successful.";
}