FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How do i execute an sql file in a php script?





[FuN]goku
I need to know how to execute an sql file from a php script. navicat is too slow to get all these in, sql file is over 50000 kb so phpmyadmin is no good... so i needa know how i can execute it from my web space in a php script.
Thanks in advance.
hexkid
[FuN]goku wrote:
I need to know how to execute an sql file from a php script. navicat is too slow to get all these in, sql file is over 50000 kb so phpmyadmin is no good... so i needa know how i can execute it from my web space in a php script.
Read the file line by line (fopen(), feof(), fgets(), fclose()) until you have a complete SQL statement (maybe with a check for ";"???). Execute the statement. Repeat until there are no more lines in the file.
Code:
<?php
########
# NOT TESTED
# NEEDS VALIDATION

$f = fopen('BIG_SQL_FILE');
$sql = '';
while (!feof($f)) {
  $line = fgets($f);
  $sql .= $line;
  if (substr(trim($line), -1) == ';') {
    mysql_query($sql);
    $sql = '';
  }
}
fclose($f);
?>


[FuN]goku wrote:
Thanks in advance.
You're welcome in retrospect.
[FuN]goku
not exactly working. i edited it for the sql.

Quote:

<?php
include 'db.php';

$con = mysql_connect($host,$user,$pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $con);

$f = fopen('mydb.sql');
$sql = '';
while (!feof($f)) {
$line = fgets($f);
$sql .= $line;
if (substr(trim($line), -1) == ';') {
mysql_query($sql);
$sql = '';
}
}
fclose($f);
?>

but gives me these errors all the way through every query.
Code:

Warning: feof(): supplied argument is not a valid stream resource in /home/xxx/public_html/sql.php on line 13

Code:

Warning: fgets(): supplied argument is not a valid stream resource in /home/xxx/public_html/sql.php on line 14
hexkid
Well ... fopen() takes two (or 3, or 4) parameters and I only wrote one
Code:
$f = fopen('mydb.sql', 'rb');


And fopen() can fail. It should be checked
Code:
if (!$f) die('Unable to open file "mydb.sql".')
Related topics
Executing a .sql file through PHP
Phoenix free PHP scripts
error in my PHP script
Inserting data to MYSQL with a PHP script
Importing/executing a BIG .SQL File
Preventing bad PHP script calls
Please help. php script installation error
Connected to another part of a php script.
Browser close run a php script.
vBulletin Config.php script
Using free php script
php script for Changing file permissions & owner
Uploading an Image using PHP script - RESOLVED
Run a php Script with a Cron Job
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.