PHP Script to Run SQL file
am 28.07.2007 23:26:42 von Arena Servers
------=_NextPart_000_005B_01C7D166.5FB8BB80
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
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:
$dbhost =3D '*****';
$dbuser =3D '*****';
$dbpass =3D '*****';
$conn =3D mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error =
connecting to mysql');
$dbname =3D '*****';
mysql_select_db($dbname);
// function create_tables() {
$filename =3D "core_sql.php";
$fd =3D fopen ($filename, "r");
$sql_data =3D 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 =3D $match[1];
preg_match_all("/create(.*?)myisam;/si", $sql_data, $result );
$sql_table =3D 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
------=_NextPart_000_005B_01C7D166.5FB8BB80--
Re: PHP Script to Run SQL file
am 30.07.2007 02:37:13 von dmagick
Arena Servers wrote:
> 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...
mysql_connect(....);
$queries = array();
$queries[] = 'create table abc(a int)';
$queries[] = 'create table def(a int, b text)';
foreach ($queries as $query) {
$result = mysql_query($query);
if (!$result) {
echo 'Unable to run query ' . $query . ': ' . mysql_error() . '
';
exit;
}
}
--
Postgresql & php tutorials
http://www.designmagick.com/
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: PHP Script to Run SQL file
am 30.07.2007 12:02:00 von Goltsios Theodore
In an another case that you have exported the database using mysqldump
(using unix) then this could do the job:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = 'test';
$file ='/tmp/file_restore.sql';
if ($dbpass != '') {
$cmd = '/usr/bin/mysql -h '.$dbhost.' -u '.$dbuser.' -p
'.$dbpass.' < '.$file;
exec($cmd,$out,$retval);
} else {
$cmd = '/usr/bin/mysql -h '.$dbhost.' -u '.$dbuser.' < '.$file;
exec($cmd,$out,$retval);
}
?>
using the operating system. Ignore this if you just need to know how to
query using php. If that is the case consider using PDO
http://www.php.net/manual/el/ref.pdo.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = 'test';
$dbh = new PDO('mysql:host='.$dbhost.';dbname='.$db, $dbuser, $dbpass);
$query = "SELECT * FROM Table WHERE test='1'";
$sth = $dbh->prepare($query);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
?>
or some other method that php supports.
Chris wrote:
> Arena Servers wrote:
>> 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...
>
>
> mysql_connect(....);
>
> $queries = array();
> $queries[] = 'create table abc(a int)';
> $queries[] = 'create table def(a int, b text)';
>
> foreach ($queries as $query) {
> $result = mysql_query($query);
> if (!$result) {
> echo 'Unable to run query ' . $query . ': ' . mysql_error() .
> '
';
> exit;
> }
> }
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php