Lynds777 Posted November 30, 2006 Share Posted November 30, 2006 Can anyone tell me how to create a database in php and add a user with all privileges?thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/28970-create-a-mysql-database-using-php/ Share on other sites More sharing options...
CheesierAngel Posted November 30, 2006 Share Posted November 30, 2006 What database server are you using? MySQL, MsSQL, ... ?You should use the exact same queries as you would use in your database server to create a database.[code]<?php // If you're using MySQL // -- Make sure your user you are using to log into the databaseserver has the proper rights to create a database and user !!! $db = mysql_connect('yourHost', 'yourUser', 'yourPassword'); mysql_query('CREATE DATABASE mydatabasename'); ....?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28970-create-a-mysql-database-using-php/#findComment-132664 Share on other sites More sharing options...
Lynds777 Posted November 30, 2006 Author Share Posted November 30, 2006 It's a mySQL db server.I heard that some servers wont allow you to create a database in php, only through cPanel. I'm starting to think that may be the case for myself but I thought I'd ask here before I give up.I have the following code:[code]<?php$con = mysql_connect("localhost","radioact_Lynds","Password");if (!$con) { die('Could not connect: ' . mysql_error()); }if (mysql_query("CREATE DATABASE radioact_testdb",$con)) { echo "Database created"; }else { echo "Error creating database: " . mysql_error(); } mysql_select_db("radioact_testdb", $con);$sql = "CREATE TABLE testtable(FirstName varchar(15),LastName varchar(15),Age int)";[/code]I get the message:Error creating database: Access denied for user 'radioact_Lynds'@'localhost' to database 'radioact_testdb' Quote Link to comment https://forums.phpfreaks.com/topic/28970-create-a-mysql-database-using-php/#findComment-132672 Share on other sites More sharing options...
CheesierAngel Posted November 30, 2006 Share Posted November 30, 2006 This is because your user you are using to log into your database server has not the correct privileges to do so.You should contact your db administrator to get the create database privileges for your user. Quote Link to comment https://forums.phpfreaks.com/topic/28970-create-a-mysql-database-using-php/#findComment-132681 Share on other sites More sharing options...
Daniel0 Posted November 30, 2006 Share Posted November 30, 2006 You still need correct permissions, but why not use mysql_create_db() instead? Quote Link to comment https://forums.phpfreaks.com/topic/28970-create-a-mysql-database-using-php/#findComment-132684 Share on other sites More sharing options...
Lynds777 Posted November 30, 2006 Author Share Posted November 30, 2006 I am the db admin. The server I use uses cPanel and all the databases I've made do far have been done through that. Each time I make a db I have to add myself as a user with all privileges. Your reply makes me convinced my server has me blocked from doing this through php script.Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/28970-create-a-mysql-database-using-php/#findComment-132688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.