MySQL Error Function Wrapper for PHP

by damonp on February 7, 2011

in Development,PHP,SQL

I was recently asked to supply some development and debugging functionality to a web application built for a client. The following function can be configured to popup for only your IP, and gives an error message containing the SQL, line number and file for easy debugging of the error.

I like this function over my current hobbled together debug kit because the display is positioned top center of the page via CSS no matter where the error occurs. Plus it includes a hide button to hide the majority of the error output and remove button to remove the error display altogether.

    function mysql_error_wrapper($q, $sql='', $line='', $file='')   {
        if($_SERVER['REMOTE_ADDR'] != 'YOUR LOCAL IP')  return false;
<script type="text/javascript" language="JavaScript"><!--
function showhide(d) {
    if(document.getElementById(d).style.display == "none") {
        document.getElementById(d).style.display = "block";
        document.getElementById('shde').innerHTML = "hide";
    }   else {
        document.getElementById(d).style.display = "none";
        document.getElementById('shde').innerHTML = "show";    

function remove()   {
    document.getElementById('errorblock').style.display = "none";

        echo '<div id="errorblock" style="display:block;position:absolute;top:10px;left:10%;width:80%;background:#efefef;text-align:left;border:1px solid #CD0000;padding:10px;">';
        echo '<a href="#" onclick="showhide(\'sqlerror\')"><span id="shde">hide</span></a> - <a href="#" onclick="remove();">remove</a>';
        echo '<h3 style="color:#CD0000;">MySQL error: '.mysql_errno().' <small style="font-weight:normal;">Line: #'.$line.' '.basename($file).'</small></h3>';
        echo '<pre id="sqlerror" style="height:250px;overflow:auto;padding:10px;">';
        echo wordwrap(mysql_error());
        if($sql != '')  {
            echo '<h4>SQL</h4>'.wordwrap($sql);

        if($line != '') {
            echo '<h4>Line</h4>'.$line;
        if($file != '') {
            echo '<h4>File</h4>'.$file;
        echo '</pre>';
        echo '</div>';         

Usage example:

    $sql = "YOUR SQL STATEMENT";
    $q = mysql_query($sql, $db);
    if(!$q) mysql_error_wrapper($q, $sql, __LINE__, __FILE__);

{ 1 comment }

Download a Remote File Using PHP and cURL

by damonp on February 3, 2011

in PHP,Snippets

Some years back, I posted a function to download a remote file with PHP. The function was useful when cURL and other URL/remote file functions had been disabled for security reasons. At the time, cURL on PHP was relatively new and many hosting setups didn’t include the cURL PHP module.

Support for PHP-cURL is pretty standard now, so I thought I’d update the the function to use cURL now. In addition, the function no longer returns a buffer containing the file data which can cause memory issues in PHP when dealing with large files. Pass this updated function a remote URL and local file path and it will download the file and save it to the local path.

This function is useful to retrieve remote images, download HTML pages and download any remote files.

    function http_get_file($remote_url, $local_file)    {
        $fp = fopen($local_file, 'w');
        $cp = curl_init($remote_url);
        curl_setopt($cp, CURLOPT_FILE, $fp);
        $buffer = curl_exec($cp);
        return true;


Hardcode WordPress Site URL

11 January 2011

I’m working on a project for mobile devices and need to be able to setup the same WordPress install on both the .mobi and the .com domain. This snippet lets you hardcode the URL and home configurables in wp_config.php. We use one setup of configurables for the .mobi side and another set for the .com. […]

Read the full article →

Linux Grep Tips

17 November 2010

Search for red OR green: grep ‘red\|green’ files Search for searchtext at the beginning of a line in files: grep ‘^searchtext’ files Search for searchtext at the end of a line in files: grep ‘searchtext$’ files Search files for blank lines: grep ‘^$’ files Search files for US formatted phone numbers (###-###-####): grep ‘[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]’ files […]

Read the full article →

HTML Image Caption

10 November 2010

<div id="image-caption"><img style="border: 1px solid black; float: right; margin: 0 0 10px 10px;" src="/images/imagename.jpg" border="0" alt="" width="175" height="132" /> <p class="caption">HTML Image Caption</p> </div> <!– #image-caption { position: relative; } #image-caption p.caption { text-align:center; position: relative; bottom: 0px; left: 0px; margin: 0; padding: 5px 3%; background-color: #555; color: #fff; font-weight: bold; font-size:.75em; } –>

Read the full article →

Cannot find terminfo entry for ‘xterm-256color’

10 November 2010

Check if you have the terminfo file installed. find /lib/terminfo /usr/share/terminfo -name "*256*" No results? Install ncurses-term. sudo apt-get install ncurses-term

Read the full article →

Debugging Wifi Connectivity and Routing

12 June 2010

Sometimes wifi connectivity issues can be massaged a little by getting a little pushy with it… turn your internal airport card off. Leave it off for a minute then turn it back on and see if by forcing it to attempt to re-connect to your local network.  Oftentimes this will clear up minor issues. Check […]

Read the full article →

Cloudy Costa Rican Sunset

2 June 2010

Looks like it’s going to rain tonight.

Read the full article →

iPhone Goodreader and Email

28 April 2010

Goodreader is simply the best PDF / document reader around for the iPad. It allows you to download documents in a dozen different ways and store them locally on your device. Another great feature is the ability to directly email these as attachments from inside Goodreader. Unfortunately, said feature is limited somewhat by the limitations […]

Read the full article →

Enable Sendmail for PHP mail() in OSX

25 February 2010

Since this didn’t dawn on me for thirty minutes while I pursued other avenues… In /etc/hostconfig add/edit a line like: MAILSERVER=-YES- Note to self: Look for more OSX server administration clients.

Read the full article →