Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/104793-solved-creating-an-installation-file/
Share on other sites

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?

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.

 

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.

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.

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);

 

 

 

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!

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.