Re: PHP Script to Run SQL file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux