简体   繁体   中英

Cant connect to sql database using xampp as local server

I have a problem connecting to sql database:

<?php
    $servername = "localhost";
    $username = "";
    $password = "";
    $myDB = "udemy";


    // Create connection
    $link = mysqli_connect($servername, $username, $password, $myDB );

    if (mysqli_connect_error()){

        die ("There was an error connecting to the database");
    }

    $query = "SELECT * FROM users";

    if (mysqli_query($link, $query)){

        echo "Query was successfull"

    }   

?>

Error is showing when I try to connect database named "udemy"...

Set username and password or add a new user in your mysql server and try with those user details.

To create new user :-

To create user in MySQL/MariaDB 5.7.6 and higher, use CREATE USER syntax :

CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'new_password';

then to grant all access to the database (eg my_db), use GRANT Syntax, eg

GRANT ALL ON my_db.* TO 'new_user'@'localhost';

Where ALL (priv_type) can be replaced with specific privilege such as

SELECT, INSERT, UPDATE, ALTER etc

Then to reload newly assigned permissions run:

FLUSH PRIVILEGES;

Now set newly created username and password in your code.

Hope it helped.

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