I have php code I'm using to import a .sql file into a mysql database. The first thing the code does is check for the file and if it exists, then it drops several tables and imports the sql file (replacing the dropped tables). As you can imagine, one time the sql file was invalid, but the code marched on and dropped the tables. Is there a way to verify the validity of a sql file using php?
This needs to have some amount of automation for a couple of reasons. I don't always do the import. After the import, the code immediately uses the new tables to update/create/delete records in other tables. So I can't have a workflow where I stop the process to check if it was a success; I need a way for php to verify the success.
This is the code being used to import the sql file:
$templine = '';
$lines = file($filename);
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '') continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';') {
$wpdb->query($templine);
$templine = '';
}
}
Interesting. I can think of two ways.
One approach is to have an alternative database to use as a test, connect to that database, import the sql into this database first, verify it worked with a query. If that passes, then connect to main database and proceed as usual.
A different approach is to export the existing database into a sql file. After importing the sql file, verify if it worked with a query, if not, import the old database with the exported sql file.
All of this can be automated to run in 1 step.
Related
Due to nda at work i can't really go into much of the other code but this is the snipit of code i'm questioning
while (($data = fgetcsv($handle,',')) !== FALSE) {
$csvstuff->execute($data);
$affectedRows +=1;
//echo "Test Rows:".$affectedRows."\n";
}
My code executes properly into the db as it should. but is there anyway to protect against the csv content that comes in if it has a Erroneous
,
In the csv string it will break my code. Is there away to check for this and prevent that from happening?
EDIT 1: I should also note that the vendor is sending this csv over via a ftp every night so its coming from an automated system I don't control their data output of the file
What would be the best way to take an .sql file that includes schema and table creation statements and use it to create new databases from within CodeIgniter? What I want to be able to do is use this .sql file as a blueprint for several databases with the same schema, but which may be created at any time.
I imagine I just need to be able to take this file, extract the contents and echo it out into a database query. Is there a better way to do it?
I also need to be able to inject a custom database name into the statements before submitting the query. How would I go about this? Just have a placeholder keyword and do a preg replace with the database name?
Just to ensure all databases are maintained synchronously, I thought perhaps this blueprint schema should be added to CodeIgniter as a module. That way if I need to alter the schema, I can upload a new version of the module with the updated .sql file and perform the necessary migrations across all the databases. Sound reasonable? If so, how would I go about this?
I have done this (run a .sql file) before, and I used this;
$sql = read_file('path/to/file.sql');
$final = '';
foreach(explode("\n", $sql) as $line)
{
if ( isset($line[0]) && $line[0] != '#' )
{
$final .= $line . "\n";
}
}
foreach (explode(";\n", final) as $sql)
{
if ($sql)
{
$this->db->query($sql);
}
}
I want to create a utility in PHP like phpMyAdmin's import option, which should allow database updates to the remote server via a .sql file without creating a new database.
Since it's a client side utility, access to cpanel is not allowed.
The app has two kinds of working environments, offline & online.
If the client works offline, they need to take the backup of database and should update the database with remote server similar for online.
Then they have to update the database of remote server.
Solution 1
If you are running your PHP on a Linux system, you can try using the 'mysql' command itself. However please note that your PHP installation has the permission to run "system" commands, like system(), exec() etc.
So here is what I mean to say:
system("mysql -U{db_user_name} -h{db_host} -P{db_password} < {full_path_to_your_sql_file}");
Please replace,
{db_user_name} with the DB username,
{db_host} with the DB host,
{db_password} with the DB password,
{full_path_to_your_sql_file} with the path to your SQL file.
And this of course requires the SQL file to be uploaded.
Solution 2:
Read the SQL file line by line and while reading execute each statement using PHP's standard MySQL library. Something like:
$arrFile = file("full_path_to_sql_file.sql", FILE_IGNORE_NEW_LINES);
foreach ($arrFile as $q) {
mysql_query($q);
}
However, this might not be as simple as it seems. If your SQL file has comments and other .sql specific statements, you might need to put checks to ignore them. Or better if the SQL file contains nothing but SQL statements.
You can use a regular upload script to obtain the .sql file, make sure you sanitize appropriately the input string to obtain only the .sql file and text type,
move_uploaded_file($_FILES["file"]["tmp_name"],"tmpdb/" . $_FILES["file"]["name"]);
Once you have that, you can either preset their db settings defining the db using
mysql_select_db('dbname');
Then just open the sql file with fopen(); slap that sucker in a variable
$file = fopen("userdb.sql","r");
$usersql = fread($file, 5);
fclose($file);
then just throw it in a mysql_query();
$uploaddb = mysql_query($usersql) or die(mysql_error());
Those are the concepts I would suggest, alternatively you can use shell exec but then that just opens up other security concerns.
You may want to consider using BigDump?
Eventually,I've got answer for my question myself.I've just pasted the php coding without config and other stuff.
$filename ="test.sql";
mysql_select_db("test");
//truncate the database.
$result_t = mysql_query("SHOW TABLES");
while($row = mysql_fetch_assoc($result_t))
{
mysql_query("TRUNCATE " . $row['Tables_in_' . $mysql_database]);
}
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
I have almost 100MB of example.sql file. (Actually data export file from another database)
I want to give user the ability to run this file through some interface in my application.
Can you please guide me how can i do that? Should i treat it as simple text file? Either there is any quick way???
In other words, I want to add same functionality as we have in phpMyAdmin, The import functionality.
If you can refer me some class on PHPclass.org that will be great.
function import_file($filename){
if ($file = file_get_contents($filename)){
foreach(explode(";", $file) as $query){
$query = trim($query);
if (!empty($query) && $query != ";") {
mysql_query($query);
}
}
}
}
can be called with
import_file("files/files.sql");
However, this function will not work properly if file includes semicolon ; somewhere else than at the end of the query
Create a file upload form that allows trusted users to upload the file to your server. Then call the mysql command-line tools from PHP to import the data into your database. Doing it by trying to explode(";", ...) will fail if there are any quoted semicolons within the imported data.
I want the following functionality using php
I have a csv file. Each file corresponds to a row in my database
There is a html form that will allow me to choose the csv file.
Then once the form is submitted, I must parse the csv file and insert data into the db accordingly
How do I go about doing this ?
Reading a CSV file can generally be done using the fgetcsv function (depending on your kind of CSV file, you might have to specify the delimiter, separator, ... as parameters)
Which means that going through you file line by line would not be much harder than something like this :
$f = fopen('/path/to/file', 'r');
if ($f) {
while ($line = fgetcsv($f)) { // You might need to specify more parameters
// deal with $line.
// $line[0] is the first column of the file
// $line[1] is the second column
// ...
}
fclose($f);
} else {
// error
}
(Not tested, but example given on the manual page of fgetcsv should help you get started)
Of course, you'll have to get the correct path to the uploaded file -- see the $_FILE superglobal, and the section on Handling file uploads, for more informations about that.
And, to save the data into your database, you'll have to use the API which suits your DB engine -- if using MySQL, you should use either :
mysqli
Note that you should prefer mysqli, instead of the old mysql extension (which doesn't support features added in MySQL >= 4.1)
or PDO