0

I'm trying find a way to create mysql users with passwords using php code, i.e. without using phpmyadmin, mysql wizard or any else.

Is there a way to do this?

3
  • Do you want ot create a database table with columns username and password or you want to insert data into these existing columns using PHP? Commented Jun 5, 2011 at 16:31
  • @Husar i want to create database which can content the tables , thanks . Commented Jun 5, 2011 at 16:54
  • $me please see my answer Commented Jun 5, 2011 at 17:06

3 Answers 3

4

you can do this with MySQL Queries:

CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password_for_new_username';

See also http://crunchbang.org/archives/2008/04/17/create-mysql-user-accounts-from-the-command-line/

Hope that helps you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, you mean ,, how can i use this code ? like this ? mysql_query('CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password_for_new_username''); ?? or what ??
mysql_query("CREATE USER 'myUserName'@'localhost' IDENTIFIED BY 'mySecretPassword';"); would work, yes.
It depends on if you're already connected to MySQL. If not, you need to run mysql_connect(). I think you need to learn a bit more basics. Search Google for PHP and MySQL Tutorials.
2

You can send any of the account management commands to the database just like any other query. Of course, it may not be good from a security perspective if your website user account has the privileges in the database to do these things. (Accounts used for public application purposes should generally be kept to minimum necessary privileges.) But that's another matter entirely.

7 Comments

Thanks, how can i use this code ? like this ? mysql_query('CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password_for_new_username''); ?? or what ??
@Programmer4me: Well, how do you currently send SQL queries to the database from your PHP code? It's been a while since I've written anything in PHP, but your example looks accurate. You'd send the command just like any other query. (Note however that your example has syntax errors in the string literal, since you start it with a single quote and then use single quotes in the string.)
Thanks, that means its not need mysql_connect('localhost','root',''); .. right ? Thank you :) .
@Programmer4me: You'll need to open a connection to the database in order to issue any queries or commands of any kind to it.
|
1

The SQL code for this would be:

 CREATE DATABASE db_name; 
 USE db_name;
 CREATE TABLE users(id int not null auto_increment, username varchar(20), password varchar(50));

But I really don't know why would you like to do this trough PHP though. Can you please explain why you need this code for?

Also it will definitely help if you have at least basic knowledge of SQL syntax (tutorials should t be hard to find with a simple search...).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.