Does anyone have a code snippet for...
Moderator: General Moderators
Does anyone have a code snippet for...
Hi,
I need a code to upload .sql files and execue them in a database.
Pretty much like the restore function in forum softwares, so I just upload a file and the queries are then executed.
I need a code to upload .sql files and execue them in a database.
Pretty much like the restore function in forum softwares, so I just upload a file and the queries are then executed.
Last edited by Jumba on Wed Apr 11, 2007 12:39 pm, edited 1 time in total.
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
phpMyAdmin is a great, free web application that has this function.
yea, I know, but the problem is, im making a site, but its not for me, so I dont want that person to go in phpmyadmin, so if just uploading the sql file to update it (So all the database details are in a php file) then that person cant mess with my other databases.jayshields wrote:phpMyAdmin is a great, free web application that has this function.
If you give user the ability to upload and execute SQL then you might as well give them full access to PHPMyAdmin. The will have the ability to execute any SQL they want including stuff that may affect your other databases.
You need to have user level security setup so that the user has their own MySQL password and login, then you can have the sql uploader run the sql with their credentials, or give them access to phpmyadmin with the mysql login/pass.
You need to have user level security setup so that the user has their own MySQL password and login, then you can have the sql uploader run the sql with their credentials, or give them access to phpmyadmin with the mysql login/pass.
Begby wrote:If you give user the ability to upload and execute SQL then you might as well give them full access to PHPMyAdmin. The will have the ability to execute any SQL they want including stuff that may affect your other databases.
You need to have user level security setup so that the user has their own MySQL password and login, then you can have the sql uploader run the sql with their credentials, or give them access to phpmyadmin with the mysql login/pass.
Oh, well, I found this
viewtopic.php?t=66219
Didn't knwo you could do that, so I'm going to give it a try
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
I have one, but its not working well, I get an error, But I have no idea what's wrgon because when I upload it in phpmyadmin it works perfectly well, so I decided to just nevermind that script and look for another onejayshields wrote:To be honest I think it would be pretty easy to code a webpage which allows an .sql file upload and executes it on a given database logged in as a set user.
btw, this is the script : http://lnl.yurx.com/insert.php (the sql file is http://lnl.yurx.com/punten.sql)
I tried almost everything I can do, but nothing worked = /
btw, bigdump isn't working, I got an Internal Server Error. The host is probably blocking it I guess
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
viewtopic.php?t=37400&highlight= may be of interest.
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
You could post your existing code in here or post it in a new thread and I'll be happy to help you with it.Jumba wrote:I have one, but its not working well, I get an error, But I have no idea what's wrgon because when I upload it in phpmyadmin it works perfectly well, so I decided to just nevermind that script and look for another one
Ok, this is the code
This is the sql query http://www.lnl.yurx.com/punten.sql
And I get this error
And uhm, feyd, about that thread, I don't really get it, is that the fix to the error I get? ---^
Code: Select all
<?php
// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for your server.
$mysqlLink = mysql_connect( 'localhost' , 'username' , 'password' );
// Select mySQL Database.
mysql_select_db( 'lnlyurx_punten' , $mysqlLink );
// Create Query To Create Table.
$sqlfile = "punten.sql";
$handle = fopen($sqlfile, "r");
$sql_query = fread($handle, filesize($sqlfile));
fclose($handle);
// Issue Query.
mysql_query($sql_query, $mysqlLink) or die(mysql_error());
// Close mySQL Connection.
mysql_close( $mysqlLink );
?>This is the sql query http://www.lnl.yurx.com/punten.sql
And I get this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; # ---------- CREATE TABLE `Table1` ---------- CREATE TABLE `Table1` ( `ID` ' at line 6
And uhm, feyd, about that thread, I don't really get it, is that the fix to the error I get? ---^
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
You need to implement something to skip lines that start with a #, as they are comments.
It would probably be easier chop the file into segments which contain each seperate query and then iterate through them all whilst executing them. You will most definately need some regular expressions for this, as you will need to locate the semi-colons which indicate the end of each query.
It would probably be easier chop the file into segments which contain each seperate query and then iterate through them all whilst executing them. You will most definately need some regular expressions for this, as you will need to locate the semi-colons which indicate the end of each query.