-1

I am trying to connect to mysql database with php code but it gives me this error: Warning: mysqli_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'test1' in C:\xampp\htdocs\imobiliare\server.php on line 8

Warning: mysqli_query() expects parameter 1 to be mysqli, bool given in C:\xampp\htdocs\imobiliare\server.php on line 11

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\imobiliare\server.php on line 13

<?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>";
 }
 }

  ?>

I would like to echo the results

S C
  • 73
  • 7
  • 1
    In your example your username and password is completely empty. You need to use proper MYSQL user who has access to table `test1` – Dharman Jan 19 '19 at 14:33
  • Thanks i solved it...i logged in with root wich has all privileges granted – S C Jan 19 '19 at 14:46
  • 2
    Keep in mind that you should not use root in your projects. Create a user which has the minimum permissions required for the database you are working on. – Dharman Jan 19 '19 at 14:48

1 Answers1

0

You need to grant the access to the database test1 by running the command on the MySQL prompt

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

Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34