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

Securing a LAMP Server with mod_security
osCommerce Easypopulate File Creator Class
CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc

Most Popular Posts

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

Archive for March, 2008

Next Entries »

PHP MySQL Query Function with Easy Error Message

Monday, March 3rd, 2008

This is a simple mysql query function that gives you a nice understandable error message if something goes wrong. You probably would not want this kind of error reporting on a live site, but for testing and developing its pretty handy.

What the function does it perform the database query as normal, but if the query fails, the function gives you a full breakdown of the mysql error messsage allowing you to easily see what has gone wrong and fix it.

PLAIN TEXT
PHP:
  1. function db_query($query){
  2.  
  3. $output = mysql_query($query) or die('
  4. <h1 style="color: red">Uh Oh......MySQL Error:</h1>
  5. <h3>Query:</h3>
  6. <pre>' . htmlentities($query) . '</pre>
  7. <h3>MySQL Error:</h3>
  8. ' . mysql_error() . '
  9. <hr /> <hr />');    return $output;
  10.  
  11. }

You would not want to use this query on a live site that is public facing as the error messages will help someone who is trying to break into your system. If you wanted to be able to use this function on a live site and quickly turn the error reporting on and off, we could use a definition like this:

PLAIN TEXT
PHP:
  1. define(MYSQL_ERROR_REPORTING: false;);

Then change the function to be like this:

PLAIN TEXT
PHP:
  1. function db_query($query){
  2. if(MYSQL_ERROR_REPORTING){
  3. $output = mysql_query($query) or die('
  4. <h1 style="color: red">Uh Oh......MySQL Error:</h1>
  5. <h3>Query:</h3>
  6. <pre>' . htmlentities($query) . '</pre>
  7. <h3>MySQL Error:</h3>
  8. ' . mysql_error() . '
  9. <hr /> <hr />');
  10. }else{
  11. $output = mysql_query($query);
  12. }
  13. return $output;
  14. }

Then if you need to see the mysql errors, you can quickly edit the definition to allow mysql error reporting by changing the definition to true.

We would always recommend developing and debugging web sites on the localhost and only making changes to the live site once you are sure that everything works. However sometimes for whatever reason it is hard to replicate an error on the localhost so you really need to see what's happening on the live site.

Alternatively if you are developing an internal system that will only be accessed by your own staff, then some built in error reporting helps you to fix problems that may occur, and also helps your more savvy staff to understand what has gone wrong and give you a good idea so that you can fix it.

Posted in php | No Comments »

Next Entries »
  • 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
    • security
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • September 2008
    • 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

    • HOSTS file, Vista and Blocking Unwanted Adverts
    • Using cURL with XAMPP
    • Faster File Browsing with Tabs for Ubuntu
    • Securing a LAMP Server with mod_security
    • Running Internet Explorer under Ubuntu Linux
    • Get Name from Email Address
    • PHP MySQL Query Function with Easy Error Message
    • Advanced PHP Debug Function
    • Product Feed Integration and Scraping Products from Supplier Web Sites
    • PHP Email Attachment Function

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

Freelance PHP Web Design UK Commercial Web Design