Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc
Magento UK
PHP Cached Download Function

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

Archive for the 'mysql' Category

PHP Generated SQL

Wednesday, May 21st, 2008

If you find yourself laboriously building SQL queries by typing each field = 'value' statement... think again.

Imagine this:

PLAIN TEXT
PHP:
  1. $sql_query = mysql_query("select * from table");
  2.  
  3. $insert_elsewhere_sql = "INSERT INTO other_table SET ";
  4.  
  5. while($s = mysql_fetch_assoc($sql_query)){
  6.     foreach($s as $k=>$v){
  7.     $insert_elsewhere_sql .= "$k = '$v', ";
  8. }
  9.  
  10. $insert_elsewhere_sql = substr($insert_elsewhere_sql, 0, -2);

This will generate a valid SQL insert statement with all of your field, value pairs set up.

Time saver or what?

Posted in mysql, php | No Comments »

MySQL Dump Partial Restore - Split MySQL Dump into Tables

Wednesday, April 9th, 2008

MySQL dumps are often used as a quick and easy way of backing up an entire MySQL database in one go. However, they are only really designed to restore whole databases as well. So what if you only want to restore certain tables?

I have written this nice little PHP script which will take your dump file and split it into lots of little sql files - one for each table. You can then restore which ever tables you want.

Simply create a php file in the same directory as your dump file. Name it something easy to remember. Then go to it in your web browser, input the name of the dump file and click go.

As this is a fairly massive task (depending on the size of your dump file) you may find it works better on your local test server with some generous php_ini settings for max_execution_time etc.

PLAIN TEXT
PHP:
  1. <?php
  2. /*MySQL Dump Split to Tables
  3. * script supplied by Edmonds Commerce
  4. * www.edmondscommerce.co.uk
  5. * if you like it, please link back with a do-follow
  6. */
  7. echo '<h1>MySQL Dump Split to Tables</h1>';
  8. set_time_limit(600);
  9. if(!isset($_GET['dump_file'])){
  10.     echo '<form><b>Dump File Path:</b> <input name="dump_file"> <input type="submit" value="go"></form>';
  11. }else{
  12.     $handle = @fopen($_GET['dump_file'], "r");
  13.     if ($handle) {
  14.         while (!feof($handle)) {
  15.             $line = fgets($handle);
  16.             if(strstr($line, 'CREATE TABLE')){
  17.                 if(isset($out)){
  18.                     fclose($out);
  19.                     unset($out);
  20.                 }
  21.                 preg_match('%CREATE TABLE `(.+?)`%', $line, $matches);
  22.                 $table_name = $matches[1];
  23.                 $out = fopen($table_name . '.sql', 'w');
  24.                 fwrite($out, $line . "\n");
  25.             }else{
  26.                 if(isset($out)){
  27.                     fwrite($out, $line . "\n");
  28.                 }
  29.             }   
  30.         }
  31.         fclose($out);
  32.         fclose($handle);
  33.     }   
  34. }
  35. echo '<h1>Done</h1>'
  36. ?>

Posted in mysql, php | 3 Comments »

MySQL DB Admin GUI for Windows / XAMPP

Tuesday, March 25th, 2008

If you are using windows (with XAMPP) to develop your MySQL applications, you might currently be using phpMyAdmin for your DBA tasks. The phpMyAdmin package is an awesome tool and is a must have, however due to the fact that it runs under Apache and PHP - it can be tricky to use when dealing with big database dumps or other heavy processing.

At this stage, you may decide that the best thing to do is to hit the command line. That's fair enough if you don't mind using command line applications. If however you fancy keeping it GUI, then I can highly recommend using the free community edition of SQLyog.

This open source community edition is a free version of their main Enterprise Edition software which is paid for. If you are doing a lot of DBA work then you may want to consider supporting this company by handing over some cash for the fully featured software. The enterprise edition can handle external as well as local MySQL database administration, using SSH, SSL or HTTP/HTTPS tunnelling. It features all kinds of GUI functionality for building your database schema and designing queries. For the serious DBA's out there it may well be worth a look.

For the occasional big DB import / export session on the local windows host though, the community edition does seem to suffice.

You can download both the community and the enterprise edition here:

http://www.webyog.com/en/downloads.php

To compare functionalty check out this page:

http://www.webyog.com/en/sqlyog_feature_matrix.php

Posted in Windows, mysql, xampp | 1 Comment »

MySQL Add Column if Not Exists - PHP Function

Thursday, March 13th, 2008

This is a nice little function which I struggled to find elsewhere on the web.

As discussed elsewhere this is not the kind of thing that should be included in a public facing script, but for administration tools etc its pretty handy:

PLAIN TEXT
PHP:
  1. function add_column_if_not_exist($db, $column, $column_attr = "VARCHAR( 255 ) NULL" ){
  2.     $exists = false;
  3.     $columns = mysql_query("show columns from $db");
  4.     while($c = mysql_fetch_assoc($columns)){
  5.         if($c['Field'] == $column){
  6.             $exists = true
  7.             break;
  8.         }
  9.     }      
  10.     if(!$exists){
  11.         mysql_query("ALTER TABLE `$db` ADD `$column`  $column_attr");
  12.     }
  13. }

This function grabs the column information for the table, then it loops through the info looking for the column. If it finds the column then it acknowledges that it exists and ceases the loop.

After this stage, the function checks to see if exists is true or not, and if it is not true then it adds the column with the attributes defined when calling the function.

The default attributes are for a VARCHAR field up to 255 characters in length and NULL enabled.

Posted in mysql, php | 2 Comments »

MySQL Copy Table from One Database to Another

Monday, March 3rd, 2008

Sometime in MySQL you want to copy an entire table from one database to a separate database. One example of when you would want to do this is if you are making a backup of a table before you apply some kind of processing to that table. There is nothing worse than ruining a database table and losing valuable information. To make an instant backup is so easy that there really is no excuse not to make backups at important or risky stages.

Here is how to do it:

PLAIN TEXT
MySQL:
  1. DROP TABLE IF EXISTS `backup_db.backup_table`;
  2. CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`;

This will delete your existing backup table completely, then will remake it copying all structural information and content from the live_table in the live_db.

Related Blogs
http://www.phpclasses.org/browse/package/4017.html
http://www.sematopia.com/?p=61
http://www.yinfor.com/blog/archives/2008/02/mysql_backup_and_recovery_meth.html

Posted in mysql, programming, web development | 2 Comments »

MySQL Find and Replace

Tuesday, February 5th, 2008

One of the most common things that needs to be done when tidying up a database is to bulk find and replace data in MySQL tables. This can be for things like spelling mistakes, changing categories of products or any other value held in a MySQL database table.

Its an easy thing to do though and here is the code:

PLAIN TEXT
MySQL:
  1. UPDATE [table_name] SET [field_name] = REPLACE([field_name],'[string_to_find]','[string_to_replace]');

note - nice syntax highlighting plugin used is mentioned here

Posted in mysql | No Comments »

  • RSS Feed
  • Categories

    • apache
    • barcode
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • PHP Save Images Using cURL
    • Web Developer Toolbar for Firefox
    • MySQL Copy Table from One Database to Another
    • Flash and PHP Charts and Graphs
    • Ultimate osCommerce Checkout - Fast and Friendly
    • Advanced PHP Debug Function
    • File Comparison on Ubuntu
    • Meta Title Tag and SEO
    • PHP Random Sleep Function with Flush
    • Who Needs Photoshop? PHP GD Images and Your Online Store

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design