MySql commands in a text file

Reply

Join Date: Apr 2007
Posts: 2
Reputation: DangerM0use is an unknown quantity at this point 
Solved Threads: 0
DangerM0use DangerM0use is offline Offline
Newbie Poster

MySql commands in a text file

 
0
  #1
Apr 28th, 2007
How would you execute MySql commands found in a text file? It's really beginning to bug me!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 160
Reputation: w_3rabi is an unknown quantity at this point 
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: MySql commands in a text file

 
0
  #2
Apr 29th, 2007
i dont know you want to load the sql into the mysql directly or using php?
anyway u could try this it works with oracle but i didnt try it with mysql
@filename.sql
.sql is a text file
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: MySql commands in a text file

 
0
  #3
May 1st, 2007
Originally Posted by DangerM0use View Post
How would you execute MySql commands found in a text file? It's really beginning to bug me!
If your code is just for your own server, you could try using the command line:

[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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC