Charas-Project

Off-Topic => Really Old Stuff => Archive => PHP => Topic started by: loftyD on June 14, 2009, 08:33:20 AM

Title: MySQL Code Install Script PHP Page
Post by: loftyD on June 14, 2009, 08:33:20 AM
If you want a simple mysql installing script than loading up PhpMyAdmin, I have made one for my piece of software, which I will post here soon!

1. GET MySQL CODE into .SQL FILE NAME "install.sql" (minus quotes)

Code: [Select]
-- Table structure for table `test_table`
--

CREATE TABLE IF NOT EXISTS `test_table` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `test` text COLLATE latin1_general_ci NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

2. COPY CODE INTO NEW PAGE

Code: [Select]


<html>
<head>
<title>Installation of YourApp</title>
</head>

<body>
<?php
//Script developed by loftyD. Please credit him!!!

$sub $_POST['sub'] ;
$loc $_POST['svr'] ;
$user $_POST['mysql_user'] ;
$pass $_POST['mysql_pass'] ;
$db $_POST['mysql_db'] ;
if(isset(
$sub)) {

$con mysql_connect($loc,$user,$pass) or die("Sorry invalid username or password.<br>");
$sel_dbmysql_select_db($db,$con) or die("Sorry database invalid or has not been created.<br>");
if(
$sel_db) {

$sqlFileToExecute 'install.sql' ;
$newconnect "<?php \$connect = mysql_connect('$loc', '$user', '$pass') ; $\db_selected = mysql_select_db($db, \$connect); ?>
" ;
strip_tags($newconnect) ;



        if ($conn !== false){
           // Load and explode the sql file
           $f = fopen($sqlFileToExecute,"r+");
           $sqlFile = fread($f,filesize($sqlFileToExecute));
           $sqlArray = explode(';',$sqlFile);
           
           //Process the sql file by statements
           foreach ($sqlArray as $stmt) {
              if (strlen($stmt)>3){
                 $result = mysql_query($stmt);
                 if (!$result){
                    $sqlErrorCode = mysql_errno();
                    $sqlErrorText = mysql_error();
                    $sqlStmt      = $stmt;
                    break;
                 }
              }
           }
        }

      $makefile = fopen('connect.php','w+') ;
  fwrite($makefile,$newconnect) ;
  echo "Database and SQL code successfully installed. Now delete this file immediately!" ;
  exit;
 



}


}

?>
<form action="<?PHP $_SERVER['PHP_SELF' ] ; ?>" method="POST">
MySQL Server (localhost should be fine): <input type="text" name="svr"><Br>
MySQL Username: <input type="text" name="mysql_user"><Br>
MySQL Password: <input type="password" name="mysql_pass"><Br>
MySQL Database (Please create before installing!) : <input type="text" name="mysql_db"><Br>
<input type="submit" name="sub" value="Submit">
</form>


</body>
</html>

3. PUT into WEBBROWSER and TEST!
Title: Re: MySQL Code Install Script PHP Page
Post by: Osmose on June 30, 2009, 06:51:43 AM
Actually all this will do is connect to an existing MySQL database and create a table. No offense, loftyD, but not too useful (I do like the parsing of a SQL file by lines).

If you have a server of your own and want to install MySQL, the downloads page at MySQL.com (http://dev.mysql.com/downloads/) should have just what you need (Which is the Community Server). It comes in all sorts of easy to install flavors.

As for managing and configuring your database, two of the most popular ways are phpMyAdmin (http://www.phpmyadmin.net/home_page/index.php) or, my personal preference, the MySQL GUI Tools (http://dev.mysql.com/downloads/gui-tools/5.0.html). Most default MySQL installations have a username of root and a blank password, so you can use those to log in and add databases, tables, etc to your heart's content.