<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Binary Tides &#187; mysql</title>
	<atom:link href="http://www.binarytides.com/blog/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.binarytides.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 24 Jul 2010 05:31:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Database Class to access Mysql</title>
		<link>http://www.binarytides.com/blog/php-database-class-to-access-mysql/</link>
		<comments>http://www.binarytides.com/blog/php-database-class-to-access-mysql/#comments</comments>
		<pubDate>Fri, 01 May 2009 16:26:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=55</guid>
		<description><![CDATA[This is a simple class that evolved out of the php code I wrote so far.
It has the following functions :
1. dbms() &#8211; The constructor to do the initialisation like connecting to the database etc.
2. query($query) &#8211; The method to take a sql string and perform the query.
3. close() &#8211; Close the database connection
4. backup() [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple class that evolved out of the php code I wrote so far.<br />
It has the following functions :</p>
<p>1. <strong>dbms()</strong> &#8211; The constructor to do the initialisation like connecting to the database etc.<br />
2. <strong>query($query)</strong> &#8211; The method to take a sql string and perform the query.<br />
3. <strong>close()</strong> &#8211; Close the database connection<br />
4. <strong>backup()</strong> &#8211; Create a backup of the database as an sql file and then zip it.<br />
5. <strong>restore()</strong> &#8211; Restore database from a zip file which was created using the previous function backup()<br />
6. <strong>get_last_insert_id()</strong> &#8211; Get the id from the last INSERT statement using the LAST_INSERT_ID() function of mysql database.<br />
7. <strong>indb($table , $field , $value)</strong> &#8211; Checks whether a value of $value already exists in the $field column of a table $table. e.g. checking whether a username or an email already exists or not.</p>
<p><span id="more-55"></span></p>
<p><strong>Code</strong> :</p>
<pre class="brush: php;">
&lt;?php
class dbms {
 #DB Configuration parameters
 var $host = 'localhost';
 var $username = 'root';
 var $password = 'secret';
 var $db = 'projects_bookmark';
 #Connection object to be used in this object
 var $connection;
 #Constructor
 function dbms() {
  #Attempt to connect to database
  $this-&gt;connection = mysql_connect($this-&gt;host, $this-&gt;username, $this-&gt;password);
  #Check connection validity
  if (!$this-&gt;connection) {
   die (&quot;Could not connect to the database:
&quot;. mysql_error($this-&gt;connection));
  }
  #If connection was successful then select the database
  $db_select = mysql_select_db( $this-&gt;db , $this-&gt;connection );
  #Check database selection validity
  if (!$db_select) {
   die (&quot;Could not select the database:
&quot;. mysql_error($this-&gt;connection));
  }
 }
 #Function to perform a query
 function query($query) {
  $result = mysql_query( $query , $this-&gt;connection);
  if (!$result) {
   die (&quot;Could not query the database:
 $query &quot;. mysql_error());
  }
  return $result;
 }
 #Method to close
 function close() {
  mysql_close($this-&gt;connection);
 }
 #Function to backup database to a zip file
 function backup() {
  $suffix = time();
  #Execute the command to create backup sql file
  exec(&quot;mysqldump --user={$this-&gt;username} --password={$this-&gt;password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$this-&gt;db} &gt; backups/backup.sql&quot;);

  #Now zip that file
  $zip = new ZipArchive();
  $filename = &quot;backups/backup-$suffix.zip&quot;;
  if ($zip-&gt;open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
   exit(&quot;cannot open &lt;$filename&gt;\n&quot;);
  }
  $zip-&gt;addFile(&quot;backups/backup.sql&quot; , &quot;backup.sql&quot;);
  $zip-&gt;close();
  #Now delete the .sql file without any warning
  @unlink(&quot;backups/backup.sql&quot;);
  #Return the path to the zip backup file
  return &quot;backups/backup-$suffix.zip&quot;;
 }
 #Function to restore from a file
 function restore($path) {
  $f = fopen('restore/temp.sql' , 'w+');
  if(!$f) {
   echo &quot;Error While Restoring Database&quot;;
   return;
  }
  $zip = new ZipArchive();
  if ($zip-&gt;open($path) === TRUE) {
   #Get the backup content
   $sql = $zip-&gt;getFromName('backup.sql');
   #Close the Zip File
   $zip-&gt;close();
   #Prepare the sql file
   fwrite($f , $sql);
   fclose($f);

   #Now restore from the .sql file
   $command = &quot;mysql --user={$this-&gt;username} --password={$this-&gt;password} --database={$this-&gt;db} &lt; restore/temp.sql&quot;;
   exec($command);

   #Delete temporary files without any warning
   @unlink('restore/temp.sql');
  }
  else {
   echo 'Failed';
  }
 }
 #Function to get the last insert ID
 function get_last_insert_id() {
  $results = $this-&gt;query(&quot;SELECT LAST_INSERT_ID()&quot;);
  $r = mysql_fetch_array($results);
  return $r[0];
 }
 #Function to check whether a value exists in some column of some table
 function indb($table , $field , $value) {
  $db = new dbms();
  $results = $db-&gt;query(&quot;SELECT $field FROM $table WHERE $field = '$value' &quot;);
  if(mysql_num_rows($results))
   return true;
  else
   return false;
 }
}
?&gt;
</pre>
<p>Simple!</p>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=55&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/php-database-class-to-access-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restore Mysql Database from a sql or zip file using PHP</title>
		<link>http://www.binarytides.com/blog/restore-mysql-database-from-a-sql-or-zip-file-using-php/</link>
		<comments>http://www.binarytides.com/blog/restore-mysql-database-from-a-sql-or-zip-file-using-php/#comments</comments>
		<pubDate>Fri, 01 May 2009 12:10:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=54</guid>
		<description><![CDATA[In the previous post we saw how the backup of a database can be taken in the form of a sql file and then zipped.

In this example the same zip file would be used to restore the database from it.
Code  :

#Function to restore from a file
function restore($path) {
  $f = fopen('restore/temp.sql' , 'w+');
 [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous <a href="http://prasshhant.blogspot.com/2009/05/backup-mysql-database-with-php-and-zip.html">post</a> we saw how the backup of a database can be taken in the form of a sql file and then zipped.</p>
<p><span id="more-54"></span></p>
<p>In this example the same zip file would be used to restore the database from it.</p>
<p><strong>Code </strong> :</p>
<pre class="brush: php;">
#Function to restore from a file
function restore($path) {
  $f = fopen('restore/temp.sql' , 'w+');
  if(!$f) {
   echo &quot;Error While Restoring Database&quot;;
   return;
  }
  $zip = new ZipArchive();
  if ($zip-&gt;open($path) === TRUE) {
   #Get the backup content
   $sql = $zip-&gt;getFromName('backup.sql');
   #Close the Zip File
   $zip-&gt;close();
   #Prepare the sql file
   fwrite($f , $sql);
   fclose($f);

   #Now restore from the .sql file
   $command = &quot;mysql --user={$username} --password={$password} --database={$db} &lt; restore/temp.sql&quot;;
   exec($command);

   #Delete temporary files without any warning
   @unlink('restore/temp.sql');
  }
  else {
   echo 'Failed';
  }
}   
</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=54&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/restore-mysql-database-from-a-sql-or-zip-file-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backup mysql database with php and zip it</title>
		<link>http://www.binarytides.com/blog/backup-mysql-database-with-php-and-zip-it/</link>
		<comments>http://www.binarytides.com/blog/backup-mysql-database-with-php-and-zip-it/#comments</comments>
		<pubDate>Fri, 01 May 2009 11:59:00 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.binarytides.com/blog/?p=53</guid>
		<description><![CDATA[PHP can be used to backup a mysql database and make a zip file of it.
mysqldump is the utility which can be used to perform this function of backing up a mysql database as sql file. The command would be like this :
mysqldump &#8211;user={$username} &#8211;password={$password} &#8211;quick &#8211;add-drop-table &#8211;add-locks &#8211;extended-insert &#8211;lock-tables &#8211;all {$db} &#62; backups/backup.sql

$username &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>PHP can be used to backup a mysql database and make a zip file of it.</p>
<p>mysqldump is the utility which can be used to perform this function of backing up a mysql database as sql file. The command would be like this :<br />
<strong>mysqldump &#8211;user={$username} &#8211;password={$password} &#8211;quick &#8211;add-drop-table &#8211;add-locks &#8211;extended-insert &#8211;lock-tables &#8211;all {$db} &gt; backups/backup.sql</strong></p>
<p><span id="more-53"></span></p>
<p>$username &#8211; mysql username<br />
$password &#8211; mysql password<br />
$db &#8211; name of the database to backup</p>
<p>In the above example backups/backup.sql is the path to the sql file. Next this backup.sql file is zipped using the ZipArchive library class of php.</p>
<p><strong>Code</strong> :</p>
<pre class="brush: php;">
#Function to backup database to a zip file
function backup()
{
  $suffix = time();
  #Execute the command to create backup sql file
  exec(&quot;mysqldump --user={$username} --password={$password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$db} &gt; backups/backup.sql&quot;);

  #Now zip that file
  $zip = new ZipArchive();
  $filename = &quot;backups/backup-$suffix.zip&quot;;
  if ($zip-&gt;open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
   exit(&quot;cannot open &lt;$filename&gt;\n&quot;);
  }
  $zip-&gt;addFile(&quot;backups/backup.sql&quot; , &quot;backup.sql&quot;);
  $zip-&gt;close();
  #Now delete the .sql file without any warning
  @unlink(&quot;backups/backup.sql&quot;);
  #Return the path to the zip backup file
  return &quot;backups/backup-$suffix.zip&quot;;
} 
</pre>
<img src="http://www.binarytides.com/blog/?ak_action=api_record_view&id=53&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.binarytides.com/blog/backup-mysql-database-with-php-and-zip-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
