Re: PHP Script to Run SQL file
| From: | Goltsios Theodore | Date: | Mon, 30 Jul 2007 10:02:00 +0000 |
| Subject: | Re: PHP Script to Run SQL file | ||
| References: | 1 2 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-43870@lists.php.net to get a copy of this message | ||
In an another case that you have exported the database using mysqldump (using unix) then this could do the job:
<?php
$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
<?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...<?php 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() . '<br/>'; exit;} }