For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > July 2007 > PHP Script to Run SQL file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP Script to Run SQL file
Arena Servers

2007-07-28, 6:58 pm

If it helps, after posting this question, I did some searching, and I found an installation script which creates a database and creates the tables using pure sql statements in a php file. Here's the coding...


PHP Code:
<?php

$dbhost = '*****';
$dbuser = '*****';
$dbpass = '*****';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = '*****';
mysql_select_db($dbname);

// function create_tables() {

$filename = "core_sql.php";
$fd = fopen ($filename, "r");
$sql_data = fread($fd, filesize($filename));
fclose ($fd);

preg_match_all("/create(.*?)myisam;/si", $sql_data, $result );

foreach ($result[0] as $sql_table) {
preg_match("/CREATE TABLE\s(.*?)\s\(/si", $sql_table, $match);
$tablename = $match[1];
preg_match_all("/create(.*?)myisam;/si", $sql_data, $result );
$sql_table = preg_replace("/create table\s/si", "CREATE TABLE test_", $sql_table);
}
?>
The file core_sql.php is a php file which opens and closes the PHP tags, and then underneath is just sql statements you would use on a MySQL command line. So this has to be possible. But the coding I have posted, I don't understand it. And it doesn't work for me. Perhaps someone can see something there that is not right. It was mostly built up of variables contained in the installation script but I manadged to "translate" most of it!


Arena Servers - Web Hosting
http://www.arenasmithster.co.uk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com