MishieMoo Posted May 9, 2008 Share Posted May 9, 2008 Okay so I'm trying to put online one of the scripts I've made, but I've run into a little snag. The only problem I really have is automatically creating a new database and user for the script to use. I'd like to streamline this process as much as possible, so that the installation file creates both the user and the database, but I just can't seem to figure out how, or find any tutorials which explain how to do this. I'm pretty sure it's possible, but I could be wrong. Any help would be appreciated =) Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/ Share on other sites More sharing options...
Cory94bailly Posted May 9, 2008 Share Posted May 9, 2008 Well.. it's pretty much like doing it in a bunch of separate files.. Just put it all into one file.. What are you trying to make it for? Show us what you have now.. Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536439 Share on other sites More sharing options...
MishieMoo Posted May 9, 2008 Author Share Posted May 9, 2008 I haven't actually started coding it yet because the rest is simple. The installation file is JUST for the database setup. What I need to know is: how do I create a new database and database user without connecting to the database with a preexisting user? Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536442 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 Here is how I do mine... install.php <?php // $ourFileName = "config.php"; // $ourFileHandle = fopen('../configs/'.$ourFileName, 'w') or die("can't open file"); // fclose($ourFileHandle); define('SMARTY_DIR', '../libs/'); require_once('../includes/aws.php'); $aws =& new AWS; $a = $_GET['a']; switch($a) { default: $interface = 'step1.tpl'; break; case s: $s = $_GET['s']; switch($s) { default: break; case 1: # create config.php file to include database strings $fileName = "config.php"; $fileHandle = fopen('../configs/'.$fileName, 'w') or die("can't open file"); $content = '<?php $O_EMAIL= "'.$_POST['o_email'].'"; $O_NAME= "'.$_POST['o_name'].'"; $server = "'.$_POST['db_server'].'"; $username = "'.$_POST['db_user'].'"; $password = "'.$_POST['db_pw'].'"; $dbase = "'.$_POST['db_name'].'"; ?>'; fwrite($fileHandle, $content); fclose($fileHandle); # end config.php. # begin populating database require('../configs/config.php'); mysql_connect($server, $username, $password); mysql_select_db($dbase); $text = file_get_contents("install.sql"); $inserts = explode("##END##",$text); $count = count($inserts); for($i = 0; $i<$count; $i++) { mysql_query($inserts[$i]); } # end populating database $interface = 'step2.tpl'; break; case 2: require('../configs/config.php'); $sql = "INSERT INTO users SET username = '".$_POST[textfield]."', password = '".$_POST[textfield2]."', email = '$O_EMAIL', department = 'root'"; mysql_query($sql); $interface = 'step3.tpl'; // completed setup.. insert admin username into database. break; } break; } $pg = '-eok- Installer'; $aws->assign('pgtitle',$pg); $aws->display("../templates/installer/".$interface); ?> note that mine uses smarty.. so you'll have to edit to fit your programming style. all the code is here for what you'll need though. install.sql CREATE TABLE `about_us` ( `about_txt` TEXT NOT NULL); ##END## CREATE TABLE `contact_us` ( `name` VARCHAR(255) NOT NULL, `date` DATETIME NOT NULL, `email` VARCHAR(255) NOT NULL, `comments` TEXT NOT NULL); ##END## CREATE TABLE `corporations` ( `name` VARCHAR(255) NOT NULL, `ticker` VARCHAR(255) NOT NULL, `website` VARCHAR(255) NOT NULL, `mem_cnt` VARCHAR(255) NOT NULL, `ceo` VARCHAR(255) NOT NULL, `about_txt` TEXT NOT NULL); ##END## CREATE TABLE `diplomacy` ( `name` VARCHAR(255) NOT NULL, `ticker` VARCHAR(255) NOT NULL, `website` VARCHAR(255) NOT NULL, `ceo` VARCHAR(255) NOT NULL, `a_dept` VARCHAR(255) NOT NULL, `about` TEXT NOT NULL); ##END## CREATE TABLE `departments` ( `name` VARCHAR(255) NOT NULL); ##END## CREATE TABLE `users` ( `username` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL, `department` VARCHAR(255) NOT NULL, `avatar` VARCHAR(255) NOT NULL); ##END## INSERT INTO `departments` ( `name` ) VALUES ( 'Standings' ); ##END## INSERT INTO `departments` ( `name` ) VALUES ( 'Join Alliance' ); hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536444 Share on other sites More sharing options...
realjumper Posted May 9, 2008 Share Posted May 9, 2008 http://www.php-mysql-tutorial.com/create-mysql-database-with-php.php Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536446 Share on other sites More sharing options...
MishieMoo Posted May 9, 2008 Author Share Posted May 9, 2008 Realjumper: That still doesn't solve the problem of creating a new user to connect with. How do you remotely create a new MYSQL user, without any using anything previously or is that just not possible? Radar: Tables aren't the problem. It's the MYSQL user I'm having trouble with. Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536447 Share on other sites More sharing options...
Cory94bailly Posted May 9, 2008 Share Posted May 9, 2008 Realjumper: That still doesn't solve the problem of creating a new user to connect with. How do you remotely create a new MYSQL user, without any using anything previously or is that just not possible? Radar: Tables aren't the problem. It's the MYSQL user I'm having trouble with. Why do you need to create a new user? Why not just have the script user select what DB and DB User to use? Easier. Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536449 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 Yeah creating a MYSQL user i don't think is possible through PHP... it might be, but thats just kinda an off thing to do. Though to create a database, you use: $query = "CREATE DATABASE mystuff"; $result = mysql_query($query); in order to create a user though you'll have to have access to the root username/password (not your root, the server root)... and then connect to the mysql server and then select the mysql database (name is mysql) $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv) VALUES ('localhost', 'myname', PASSWORD('mypass'), 'Y', 'Y', 'Y')"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536450 Share on other sites More sharing options...
MishieMoo Posted May 9, 2008 Author Share Posted May 9, 2008 Cory: It's supposed to be an easy to use script, where the user doesn't have to know anything about CPanel or MYSQL, so they can just run the install script and everything is done. Less room for error that way. And it's a counter script, so there will possibly be lots of tables associated with it, and I think it should have its own database. Radar: It IS possible to create users. I've used other scripts on my site that I didn't have to create the database or user for, so I know it can be done, but I lost the source files. I'm asking if anyone knows, because it is out there somewhere and my searches have revealed nothing! Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536451 Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2008 Share Posted May 9, 2008 You will find that most shared hosting accounts will not let you create databases and database users through a script (the script would need access to an existing user account that has database creation privileges.) For the most common situation, you will be limited to creating databases and users through a hosting control panel. The original question is about creating databases and users, not about creating tables and inserting data. Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536452 Share on other sites More sharing options...
MishieMoo Posted May 9, 2008 Author Share Posted May 9, 2008 Thanks PFMaBiSmAd, I wasn't sure if it was possible. There goes a real easy installation xD Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536453 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 read my post above, and more information can be found : http://dev.mysql.com/doc/refman/5.0/en/adding-users.html Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536455 Share on other sites More sharing options...
MishieMoo Posted May 9, 2008 Author Share Posted May 9, 2008 Radar, I know how to create databases. I just had a brain fart xD My question was whether or not creating a mysql user would work or not, and if so how to do it. Apparently it won't work that way so now I have to go write complicated installation instructions. Quote Link to comment https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/#findComment-536456 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.