Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is it possible to runn sql script files via php?

Status
Not open for further replies.

ShaneBrennan

Programmer
Joined
May 19, 1999
Messages
198
Location
GB
I have a file called create_db1_users.sql which has all the code to :
o drop(if exists) a table,
o create the table again with a new set of properties
o use a series of "INSERT INTO" statements to place some initial data into the table.

Now I can load then run this from phpAdmin, but I want to do it through my web front end. ie. on the click of a button/link or page load simply run the file.

Can anyone help me please as I'm new to php and I'm hitting a brick wall here ;-)

Thanks for any help you offer in advance.



Shane Brennan

'-----------------------

' Emails: shanebrennan@postmaster.co.uk


 
I know you prob dont want to do this but you could always connect to the database from your script and run the different lines of code.

include("/vweb/stockcontrol/html/config.php");// Contains connection details
$connection = ocilogon($strUsername,$strPassword,$strDB) or
die("Can't login $strDB");
$tempSQL="CREATE TABLE users etc...";
$stmt = OCIParse($connection,"$tempSQL");

ociexecute($stmt) or die ("Can't execute : $tempSQL");

 
Oracle - i presume you're using mysql like everyone else - it shouldn't be too much hassle to convert the code
 
I am using MySQL.

All I want to do is simply run a schema dump from one machine on another through php.

I can create databases, tables, insert table, etc. - but I want to automate it so I can reset a database back to it's default stage once I have finished playing with it - like a backup system where I can take a schema dump at any stage, then put the data back on - but I want to do it through PHP from anywhere.

Shane Brennan

'-----------------------

' Emails: shanebrennan@postmaster.co.uk


 
thats a bit beyond me at the moment - im just a beginner 2
 
me too :)

Shane Brennan

'-----------------------

' Emails: shanebrennan@postmaster.co.uk


 
The way I see it, you have two choices:

One way to do it is to have PHP open the SQL file, parse through the file, and execute each SQL query in the file in tern. This can be tricky -- PHP will only allow one SQL query per invocation of mysql_query(), so you have to perform each separately. Also, it is not uncommon for an SQL query to run across several lines -- knowing where one stops and the next begins is difficult.

Another way to do it is to have PHP execute the mysql command-line admin application externally, and feed mysql the SQL file. If your installation of PHP allows execution of external programs, this will likely be the easiest.

Keep in mind that however you do it, what you are doing in inherently dangerous. If you have any security holes in your system, they can be exploited and your database trashed. If you aren't careful with what you do legitimately, your database can be trashed.

Want the best answers? Ask the best questions: TANSTAAFL!
 
A third third thing to consider is that phpmyadmin is open source. Crack open the scripts and see what they do, you just want to mimick their functionality.

-Rob
 
Ooop you said phpAdmin... well, then I suggest you download phpMyAdmin, then crack open the scripts :)

-Rob
 
thanks for all the help

Shane

Shane Brennan

'-----------------------

' Emails: shanebrennan@postmaster.co.uk


 
I found an application I think called sqlog or something like that...it's on my laptop at home, I use it to copy database shema from laptop to production server, syncing the two databases.

would this help ya

if so I'll find the like and post it here tonight



jef
 
that would be fantastic!

Shane Brennan

'-----------------------

' Emails: shanebrennan@postmaster.co.uk


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top