| | |
MySql commands in a text file
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
•
•
How would you execute MySql commands found in a text file? It's really beginning to bug me!
[PHP]<?php
exec("mysql -u USER -p DBNAME < dump.sql");
?>[/PHP]
or try this class from: http://www.phpclasses.org/browse/file/12857.html
[PHP]<?php
/**
* Author : MA Razzaque Rupom (rupom_315@yahoo.com)
* Version : 1.0
* Date : 12 Feb, 2006
* Purpose : Importing valid SQL dump to DB
* Release : Released under GNU Public License
*/
class parse
{
var $file;
function parse($file)
{
$this->setFile($file);
$this->startParsing();
}
/**
* @purpose : Sets filename to be parsed
* @params $file
* @return none
*/
function setFile($file)
{
$this->file = $file;
}
/**
* @purpose : Gets filename to be parsed
* @params none
* @return filename
*/
function getFile()
{
return $this->file;
}
/**
* @purpose : Parses SQL file
* @params none
* @return none
*/
function startParsing()
{
$file = $this->getFile();
// Getting the SQL file content
$content = file_get_contents($file);
// Processing the SQL file content
$file_content = explode("\n",$content);
$query = "";
// Parsing the SQL file content
foreach($file_content as $sql_line)
{
if(trim($sql_line) != "" && strpos($sql_line, "--") === false)
{
$query .= $sql_line;
// Checking whether the line is a valid statement
if(preg_match("/(.*);/", $sql_line))
{
$query = substr($query, 0, strlen($query)-1);
//Executing the parsed string, returns the error code in failure
$result = mysql_query($query)or die(mysql_error());
$query = "";
}
}
} //End of foreach
return true;
} //End of function
} //End of class
?> [/PHP]
from the look of the class though, I think it will choke on SQL queries that have new lines - "\n" - inside queries.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- read from database and writing the contents into a text file (C)
- Copying a folder into Excel or text file (Windows NT / 2000 / XP)
- read text file (C)
- Save a text file in an csv.file with ascii? (PHP)
- Complete List of Mysql Commands for PHP tokenization script. (PHP)
Other Threads in the PHP Forum
- Previous Thread: error in myvehicle.php!
- Next Thread: Undifined Index in PHP code
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions simple sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






