我知道这个问题已被问过一百万次,但他们都没有解决这个问题。我使用PHP连接到我的localhost数据库,使用下面的PHP脚本:
<?php
// CONNECT TO THE DATABASE
$DB_NAME = 'users';
$DB_HOST = 'localhost';
$DB_USER = '********';
$DB_PASS = '********';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `members` WHERE `username`='$myusername' and `password`='$mypassword'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
// GOING THROUGH THE DATA
if($result->num_rows == 1) {
header("location:login_success.php");
}
else {
header("location:fail.php");
}
// CLOSE CONNECTION
mysqli_close($mysqli);
?>
这可以连接到数据库,但无法找到数据库'users'。您可以从下面列出的图像中看到数据库存在,但找不到它。
http://i.imgur.com/xYvUCks.png
任何有助于解决此问题的帮助将不胜感激!
答案 0 :(得分:0)
您可以在phpmyadmin
GRANT SELECT, INSERT, DELETE ON database TO username@'localhost' IDENTIFIED BY 'password';
然后尝试运行您的PHP代码。
答案 1 :(得分:0)
您正在使用过程接口将OOP接口与mysqli
混合。
试试这个:
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if ($mysqli->connect_errno) { // Changed to OOP version
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}