CodeIgniter - Create multiple databases from master SQL file - php

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);
}
}

Related

Verify SQL file before use

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.

How to make a php script that reads filenames and details from any directory and stores it on database?

I am given a lot of video files on my server folder, so adding all of them is impossible, I need to develop a php script that will read the contents of a folder and populate my database table with details such as filename, size, path, etc.
Furthermore I need to use that data for displaying the video list to users so everything must be accurate.
Please tell me how to?
I suggest you to use Iterators from SPL (Standart PHP Library) for an OOP aproach, for recursive iterations you should use DirectoryRecursiveIterator;
$directoryIterator = new RecursiveDirectoryIterator("/path/");
$recursiveIterator = new RecursiveIteratorIterator($directoryIterator);
foreach ($recursiveIterator as $filename => $fileInfo) {
$filesize = $fileInfo->getSize();
$path = $fileInfo->getPath();
//insertDataIntoMysql($filename, $filesize, $filepath);
}
See here to see how to loop through a directory and get the filenames and file sizes. (Copied and edited here for simplicity)
$folder = '/my/path/to/dir';
foreach(glob($folder) as $file){
$size = filesize($file);
echo "Name=$file, size=$size<br />";
}
Instead of using echo to display the results you will simply use mysqli or PDO functions to run an INSERT query to insert into your table. Since this is not input coming from a user it is not so imperative to use prepare and bind but it is good practice to do so (and more efficient as well).
Then when you display the video list you will once again use mysqli or PDO functions to run a SELECT query and then echo these values onto your page in appropriate HTML syntax.
I'm guessing, based on your question, that some of these concepts might be new to you. If you do not know how to program in PHP or use SQL or create HTML pages then you are going to need to spend some serious time going through tutorials and watching youtube videos and reading books.

SugarCRM save database data to file

Is there any way I can save the data of a specific table of the sugarcrm database into a doc file ?
I have a custom module with username,some notes and date. I want to write this into the database and into a file as well.
Its not just a php file. I want to use logic hooks and write the code. I want to use the logic hooks to access database and then write the data into the file.
Thanks in advance
Saving as a DOC file probably isn't the best idea, since it is primarily used for formatting information. A standard .txt file is usually what you would use for such a process.
With that said, there isn't any methods built into sugar that will let you do this. You will need to build the capability into the module.
What exactly are you attempting to accomplish? There is a very powerful auditing tool set, which is good for seeing revisions to a module object. If you are just wanting to monitor changes to the table, you can setup logging for that table/database inside of SQL.
+++Ok, if you are just looking to write to a file after saves, follow the instructions at: http://cheleguanaco.blogspot.com/2009/06/simple-sugarcrm-logic-hook-example.html for a quick how-to on getting the logic hooks working. You are going to want to make a php file that simply uses the data passed to it via the bean class, and either writes to the file directly from the data within bean, or uses the bean->id parameter to do a SQL query and write to the file from that data.
Also, is this a DOC file that is going to be immediately generated and then destroyed at the end of the transaction? Or is it more of a log file that will be persistent?
++++That is simple enough then
Where you have the Query right now, replace it with:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
*A quick note, you might need to set permissions in order to be able to read/write to the file, but those are specific to the machine type, so if you encounter errors with either do a search for setting permissions for your particular server type.
**Also, 'SELECT * FROM database.table' is going to return ALL of the rows in the entire table. This will generate a very large file, and be a performance hindrance as the table grows. You should use the bean class to update the last saved tuple:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table WHERE id = '".$focus->id."';";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
You can export/dump mysql databases into SQL files using mysqldump
mysqldump -u userName -p databaseName tableName > fileName.sql

How to execute mysql script file in PHP?

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.

How to create a temporary table in MySQL to output table data as a CSV file?

I've got a script for creating a CSV file from a database table, which works fine except that it outputs all the data in the table, and I need it to output data only for the current logged in user. The app I'm developing is for users to be able to store lists of books they've read, want to read, etc., and I want to be able to allow them to download a list of their books they can import into Excel for example. (Would a CSV file be the best option for this, given that there's also OpenOffice etc. as well as MS Excel?)
I figured out that I needed to create a temporary table to use a select query to select only records belonging to the current logged-in user. I can access the data for the current user using WHERE username='$session->username'.
I thought it would be as easy as creating the temporary table before I try to run the queries that output the data for use in the CSV file, and then drop the table afterwards, but I've tried the CSV creation code again with the temporary table created before the CSV stuff, and again the CSV file produced includes all the records for all users.
There's a configuration file that sets the database connection properties, but also the table to be used to create the file, as the variable $table, and I've set this to be the name of the temporary table I'm trying to create.
This is the code I've got:
<?
$link = mysql_connect($host, $user, $pass) or die("Cannot connect to the database." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$temp = mysql_query("CREATE TEMPORARY TABLE books_temp SELECT * FROM books WHERE username='$session->username'");
$tempresult = mysql_query($temp);
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("d-m-Y") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
$query = 'DROP TABLE books_temp';
$result = mysql_query($query);
exit;
?>
It works perfectly outputting all the data, but still outputs all the data when I try to use it with the temporary table. Incidentally, I've tried remove the instruction to drop the temporary table from the end as well, and that doesn't make any difference. Also, checking in PhpMyAdmin, it doesn't seem as though the temporary table's being created. I'm obviously doing something wrong here or missing something, but I've no idea what.
Well after a bit of a break from this I've come back to it, and as you guys said, I didn't actually need a temporary table at all.
For the sake of completeness, and incase anyone else encounters the same problem, this was the eventual solution to it.
With the when clause on the SELECT query the script works fine, I eventually discovered, but I still wasn't getting any data through (just the column names). I finally figured out the reason I wasn't getting any data was because although '$session->username' works within my PHP pages to access the name of the current user, the script, which is only linked to the page, has no way to access that session info.
Finally I worked out I needed to add in the extra variable $username and get the value for that from a query string on the URL (not quite sure how I was presuming the session info would get to the script - head wasn't in gear!). It was because the script couldn't access '$session->username' that the script wasn't working.
Rather than linking to the script with:
Download your list
I used this instead (after declaring the variable on the page):
$accname = $session->username;
and:
Download your list
Then all I had to do was add the $username variable into the top of the script and amend the SELECT query with the variable.
$username = mysql_real_escape_string($_GET['user']);
At least it all works now! (I did try some of the suggestions on the other discussion, but couldn't get any to work - either I got only the header rows or a load of errors).
I have a poor man's approach to making a CSV file provided the column types are simple. It could be done by means of the CSV Storage Engine.
USE mydb
CREATE TABLE books_csv SELECT * FROM books WHERE 1=2;
ALTER TABLE books_csv ENGINE=CSV;
INSERT INTO books_csv SELECT * FROM books WHERE username='...';
When this step is done, the file /var/lib/mysql/mydb/books_csv.CSV exists.
You can open file how you wish. BTW all fields are enclosed with double quotes.
Give it a Try !!!

Categories