【问题标题】:I am trying to connect to mysql database with php code but it gives me this errors (see below) [duplicate]我正在尝试使用 php 代码连接到 mysql 数据库,但它给了我这个错误(见下文)[重复]
【发布时间】:2019-01-19 14:20:17
【问题描述】:

我正在尝试使用 php 代码连接到 mysql 数据库,但它给了我这个错误:警告:mysqli_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'test1' in C :\xampp\htdocs\imobiliare\server.php 第 8 行

警告:mysqli_query() 期望参数 1 为 mysqli,布尔值在第 11 行的 C:\xampp\htdocs\imobiliare\server.php 中给出

警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,在第 13 行的 C:\xampp\htdocs\imobiliare\server.php 中给出 null

<?php

$servername = "localhost";
$username = "";
$password = "";
$dbname = "test1";

$conn = mysqli_connect($servername, $username, $password, $dbname);

$sql = "SELECT id, camere, zona, pret FROM apartamente";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) > 0) {
 while($row = mysqli_fetch_assoc($result)) {
  echo "id: " . $row["id"]. "Camere: " . $row["camere"]. "Zona:" .       $row["zona"]. "Pret: " . $row["pret"]."<br>";
 }
 }

  ?>

我想回显结果

【问题讨论】:

  • 在您的示例中,您的用户名和密码完全为空。您需要使用有权访问表 test1 的正确 MYSQL 用户
  • 谢谢我解决了...我用 root 登录,拥有所有权限
  • 请记住,您不应在项目中使用 root。创建一个具有您正在处理的数据库所需的最低权限的用户。

标签: php mysql xampp


【解决方案1】:

您需要通过在 MySQL 提示符下运行命令来授予对数据库 test1 的访问权限

mysql> GRANT ALL PRIVILEGES ON test1.* TO 'username'@'localhost';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2022-07-26
    • 2023-02-10
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多