I was wondering if there was a way to create a new user for my MySQL Database? I'm trying to add basic log-in functionality that will allow the user to connect to database. If user has no account, then I want to create a new one. This is what I have so far:
<html>
<head>
<title>Test</title>
</head>
<body>
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("contactsDB",$dbhandle)
or die("Could not select examples");
echo "Found contacts DB";
?>
</body>
Everything connects properly, but now, instead of having to type in the username and password (root), I want user to type their username and password. Again, if they don't have one, then I want to create a new one.
You need atleast one admin account to create a user with less privilege.
Please refer to create db and user mysql and set privileges php which is a good reference.
This is perfectly possible, but one thing I would stay clear of is logging in as root to do these commands.
With that in mind a method similar to this may work.
mysql_connect('localhost', 'user', password);
mysql_query("CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';");
mysql_query("GRANT ALL ON db1.* TO 'username'@'localhost'");
mysql_close();
If you have a dedicated database for these new users, you can easily assign it then.
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.