-3

The code

index.php file

<?php
  include_once 'includes/dbh.php';
?>
<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<?php

    $sql = "SELECT * FROM data";
    $result = mysqli_query($conn, $sql);
    $datas = array();
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $datas[] = $row;
        }
    }

    //print_r($datas);

    //foreach ($datas[0] as $data) {
    //    echo $data['text']." ";
    //}

?>

</body>
</html>

includes file

<?php

$server = "localhost";
$username = "";
$password = "";
$database = "test";

$conn = mysqli_connect($server, $username, 
$password, $database);
if (!$conn) {
    die("Connection failed: " . 
mysqli_connect_error());
}

The Warning

Warning: mysqli_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\databasetoarray\includes\dbh.php on line 8 Connection failed: Access denied for user ''@'localhost' (using password: NO)

Other info The database and php files are all correctly named.

The Goal I am trying to get an array out of a database using a tutorial I found on the internet.

The Questions Does this method even work if using correct coding? If so what can I change to make it work?

3 Answers3

1

Please connect to localhost/phpmyadmin and verify the credentials and update it here:

$server = "localhost";
$username = "";
$password = "";
$database = "test";

By default the username will be root and password will be blank.

Alpha 1
  • 4,118
  • 2
  • 17
  • 23
1

The default password for mysql is root if you want it to set another you should change it.

Kunal Raut
  • 2,495
  • 2
  • 9
  • 25
0

on localhost you shold use username is root, password blank and correct database name use following code

$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$conn = mysqli_connect($server, $username, 
$password, $database);
if (!$conn) {
    die("Connection failed: " . 
mysqli_connect_error());
}