I want to Create mysql user and database using php script
how to create mysql user using mysql_query()
<?php
mysql_query("CREATE USER 'demodemodemo'#'localhost' IDENTIFIED BY 'jayul321';");
?>
this is not working..
PHP script to create MySQL database, add user and grant privileges.
## make sure you connect first to mysql with a super user (ex: root)
mysql_connect('localhost', 'root', 'password');
$dbName = 'database_name'; // new database name
$dbUser = 'db_user'; // new user name
$dbPass = 'db_pass'; // new user password
$queries = array(
"CREATE DATABASE `$dbName` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
"CREATE USER '$dbUser'#'localhost' IDENTIFIED BY '$dbPass'",
"GRANT USAGE ON * . * TO '$dbUser'#'localhost' IDENTIFIED BY '$dbPass' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0",
"GRANT SELECT , INSERT , UPDATE, DELETE ON `$dbName` . * TO '$dbUser'#'localhost'",
"FLUSH PRIVILEGES"
);
foreach($queries as $query) {
echo 'Executing query: "'.htmlentities($query).'" ... ';
$rs = mysql_query($query);
echo ($rs ? 'OK' : 'FAIL') . '<br/><br/>';
}
Related
I am using the standard MySQL functions that PHP has built in for copying one table to an new table.
This works perfect when the tables are in the same database. But I want to copy it to another databasename with same user and password.
Any suggestions how to achive this?
(Since $database can only contain 1 databasename)
Error shown is Table 'torzahttp_rsw.torzahttp_rsw.kwaliteit' doesn't exist
torzahttp_rswis the database name, and kwaliteit the table name.
Why is the databasename used twice?
// Create a new MySQL database connection
if (!$con = new mysqli('localhost', $username, $password, $database)) {
die('An error occurred while connecting to the MySQL server!<br><br>' . $con->connect_error);
}
// Create an array of MySQL queries to run
$sql = array(
'DROP TABLE IF EXISTS `backup_db.backup_table`;',
'CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`'
);
// Run the MySQL queries
if (sizeof($sql) > 0) {
foreach ($sql as $query) {
if (!$con->query($query)) {
die('A MySQL error has occurred!<br><br>' . $con->error);
}
}
}
$con->close();
From the MySQL manual:
If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.
Your code:
$sql = array(
'DROP TABLE IF EXISTS `backup_db.backup_table`;',
'CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`'
);
should look like this:
$sql = array(
'DROP TABLE IF EXISTS `backup_db`.`backup_table`;',
'CREATE TABLE `backup_db`.`backup_table` SELECT * FROM `live_db`.`live_table`'
);
Or, just drop the backticks altogether.
I have used this solution to create an mysql dump first and then processioning it.
//DB
$user = 'xxxxx';
$password = 'xxxxx';
$host = 'localhost';
$database_old = 'old_db';
$database_new = 'new_db';
$table = 'kwaliteit';
$file = 'kwaliteit.sql';
exec('mysqldump --user='.$user.' --password='.$password.' --host='.$host.' '.$database_old.' '.$table.' > uploads/'.$file);
exec('mysql --user='.$user.' --password='.$password.' --host='.$host.' '.$database_new.' < uploads/'.$file);
unlink('uploads/'.$file);
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 (e.g. my_db), use GRANT Syntax, e.g.
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.
I'm trying to use Firebird in my PHP application. I already managed to make interbase and Firebird PDO extensions work, and the connection is established without any problems.
But when I try to make a simple select into one of the tables (select * from filial), it always returns empty, just like there's nothing recorded in the table.
I already tested my script in another database, and it worked properly, so I guess it's not a PHP problem, but I think it has something with my database.
This is how I created the database and table with ISQL:
create database 'C:\My\Database\Path.fdb' page_size 4096 user 'myuser' password 'mypass' default character set UTF8;
connect 'C:\My\Database\Path.fdb' user 'myuser' password 'mypass';
create table filial (id int not null primary key, nome varchar(45));
insert into filial (id, nome) values (1, 'test');
When I run the 'select' query in ISQL, it returns the one inserted row. But doing it with interbase or PDO, I get an empty object.
I also tried using capital letters for the table and columns names.
What am I doing wrong?
I'm running this project in a Windows 7, with WAMP and Firebird server installed.
Interbase PHP code:
$db_server = 'localhost';
$db_user = 'SYSDBA';
$db_passw = 'masterkey';
$db_name = 'C:\Users\Joao\Firebirds\Desenvolvimento.fdb';
$host = $db_server.':'.$db_name;
$dbh = ibase_connect($host, $db_user, $db_passw);
$stmt = 'select * from filial';
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
echo $row->ID.'<br />';
}
ibase_free_result($sth);
ibase_close($dbh);
PDO PHP code:
$db_server = 'localhost';
$db_user = 'SYSDBA';
$db_passw = 'masterkey';
$db_name = 'C:\Users\Joao\Firebirds\Desenvolvimento.fdb';
$str_conn='firebird:host='.$db_server.';dbname='.$db_name;
$conn = new PDO($str_conn, $db_user, $db_passw);
$q = $conn->prepare('SELECT * FROM filial;');
$q->execute();
$dados = $q->fetchAll(PDO::FETCH_OBJ);
foreach($dados as $row){
echo $row->ID.'<br/>';
}
As I'm working locally, I also put connection data.
I'm trying to DROP database and its user using PHP.
Though I could able to DROP database but failed to DROP user.
Following command which I have tried,
$cpanel_user = 'abc';
$cpanel_user_password = 'xyz';
$conn = mysql_connect($dbhost = 'localhost', $cpanel_user , $cpanel_user_password );
$sql = 'DROP DATABASE dbname';
$retval = mysql_query( $sql, $conn ); // works perfect
$drop_user = "DROP USER 'username'#'localhost'";
$retval = mysql_query( $drop_user , $conn );
Unfortunately it showing "Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation".
The user which is use for connection is cPanel user so must be having full privileges.
I refereed other threads like, MySQL: Check if the user exists and drop it but no luck.
Do I missing anything?
You should grant 'CREATE USER' Privileges to the user you used to connect to the database.
As your code, you should run this statement by the 'root' user:
GRANT CREATE USER ON *.* TO abc;
I am trying to connect to my database and when I run the code, I get an error. Can anybody tell me what is wrong also any errors in my PHP code? Thanks.
Error: No database selected
PHP Code:
include('.conf.php');
$prof = $_GET['profile'];
$prof = addslashes(htmlentities($prof));
$query = "SELECT * FROM aTable where id = '".mysql_real_escape_string($prof)."'";
$info_array = mysql_query($query, $con) or die (mysql_error()).;
while($row = mysql_fetch_array( $info_array ))
{
echo $row['num1'];
echo "</br>";
echo $row['num2'];
echo "</br>";
echo $row['num3'];
echo "</br>";
echo $row['num4'];
};
mysql_close($con);
.conf.php file:
<?php
$conf['db_hostname'] = "xxx";
$conf['db_username'] = "xxx";
$conf['db_password'] = "xxx";
$conf['db_name'] = "xxx";
$conf['db_type'] = "mysql";
$con = mysql_connect('xxx', 'xxx', 'xxx') or die (mysql_error());
$db = mysql_select_db("aTable", $con);
?>
I had that problem and solved it with prefixing the table name with the database name, so
select * from database.table
Unless you have the password incorrect and need to fix it, run a GRANT statement to grant access to your database user:
GRANT ALL PRIVILEGES ON aTable.* TO xxx#localhost IDENTIFIED BY 'password_for_xxx';
The above grants all privileges. It's often better to restrict to only what's needed. For example, if you only intend to SELECT, but not modify data,
GRANT SELECT ON aTable.* TO xxx#localhost IDENTIFIED BY 'password_for_xxx';
Update
Since you have identified the database name as dedbmysql, change your mysql_select_db() call to:
$db = mysql_select_db("dedbmysql", $con);
Following this, the GRANT statements above are probably unnecessary, as your host may have already worked out the correct database permissions.