PHP

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.

<?php
    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";
}
//--></script>

<?php
        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:

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

Popularity: 3%

{ 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.

<?php
   
    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);
       
        curl_close($cp);
        fclose($fp);
       
        return true;
    }
?>

Popularity: 13%

{ 1 comment }

PHP gethostbyname() and DNS

23 August 2007

For PHP’s gethostbyname() to work properly, the server’s DNS must be properly configured with available nameservers in /etc/resolv.conf (on Linux boxes). Without a work domain name resolution kit, gethostbyname() returns the hostname supplied to the function. Make sure /etc/resolv.conf contains several nameservers to query. The format is nameserver xxx.xxx.xxx.xxx nameserver yyy.yyy.yyy.yyy Where xxx.xxx.xxx.xxx and yyy.yyy.yyy.yyy [...]

Read the full article →

Easily View PHP Errors on Yahoo Business Hosting

6 August 2007

I don’t often use Yahoo Business Hosting, but every once and a while I have a client who is already hosting their sites there. With all large monolithic hosting companies there are caveats to using their systems… PHP especially. Yahoo Hosting does not support .htaccess files and does not allow display of PHP error messages [...]

Read the full article →

PHP Stopwatch Class to Time and Log Script Execution

18 July 2007

Timing code execution is an often overlooked debugging tool. On a current large project we are doing a lot of work interfacing with multiple SOAP implementations on multiple servers and through multiple vendors. When script execution slows down considerably (or stops responding altogether) we needed to know which SOAP calls were causing the slowdown. Using [...]

Read the full article →

Coding on a Live Site

29 June 2007

I have written several times about debugging a live site and posted snippets for working on the themes of a live WordPress install. One trick I haven’t mentioned is using the PHP error log. PHP on any production site should be configured to not display errors. I see all too often on random sites that [...]

Read the full article →

WordPress Bug – Theme Reverts to Default Theme

23 June 2007

I have been working on a new WordPress theme for this site and have noticed on several occasions that WordPress has reverted back to the default theme. Google turned up a few helpful pages: WordPress Theme Resetting Problem posted by Scott Burkett WordPress defect #3907 The problem boiled down to the fact that I was [...]

Read the full article →

Zencart Hack – Logout Customer Automatically After X Failed Payment Attempts

22 June 2007

Credit card slamming is the practice of trying hundreds or thousands of card numbers and security code combinations to find the few in the batch that will actually work. I have discussed credit card slamming here and over at the ZenCart forums several times in the past. The code snippet below can be used in [...]

Read the full article →

CodeIgniter and BambooInvoice

22 June 2007

I recently started toying around with CodeIgniter on a new project where I need a simple user login and administration system. I was initially attracted to the first two features listed: You want a framework with a small footprint. You need exceptional performance. This is the first framework I have ever stayed with after a [...]

Read the full article →

WordPress Hack to Debug Themes on a Live Site

9 April 2007

How can a WordPress them be debugged privately without showing the wizard behind the curtain to everyone else? This simple hack will use the specified theme only for one remote IP… yours. This can be used when creating a new theme by configuring WordPress to present the original theme and specifying your new theme in [...]

Read the full article →