简体   繁体   中英

Windows 10 XAMPP - PDO connection fine, cannot connect via mysqli or mysqli_connect

I am using a Windows 10 PC which has XAMPP installed (v3.2.2).

If I do this in MySQL:

select version();
-- 10.1.32-MariaDB

That confirms the version...

I have a test database called "wp", and the username and password are both set to "wp".

I can connect to the database without a problem using PDO, but I can't connect using mysqli or mysqli_connect, and I can't see why. I've wasted a few hours on this today so far. I'm trying to install Wordpress on XAMPP but the installer says it can't connect to the database, so I have been testing around this, hence this question.

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

The wp user has the correct level of access:

在此处输入图片说明

Since the same user details are used to connect via PDO, but they don't work for the other methods.

I have checked using phpinfo(); and can see that mysqli is set up as far as I can see (I searched for mysqli_connect but can't see it listed):

mysqli phpinfo

在此处输入图片说明

Am I missing something obvious?

Try this

//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.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM