Hi,
I’m a newbie to databases. What I’ve done is:
<?php $con = mysqli_connect(‘localhost’, ‘admin’, ‘password’, ‘test’) or die("Connection failed: " . mysqli_connect_errno()); ?>
Somehow, now matter what I try, it keeps giving me a connection error. Am I doing something wrong?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
MySQL users are identified by a username and host. Additionally you should only be using your MySQL admin account to administer the MySQL server itself but not to access various databases and not to read and write information that are connected to by apps. This is for security.
Since you have created a database already called test you will want to create a new user that will access that database and read/write data to it.
To do that login to mysql as your admin account and create a user with access:
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'replace-with-desired-password';
GRANT ALL PRIVILEGES ON test.* TO 'testuser'@'localhost';
Here you are creating a new user called testuser which can connect from localhost with the password you specified.
Then you are granting that user privileges to access, read, write, etc to the database test.*. When you write test.* that means you are giving this user access to the database test, and the * means all tables for that database.
You can also find more in depth information about users and permissions here: https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.