Posts Tagged: Programming


12
Mar 10

WordPress Programming Tip: Enable Database Error Reporting For Custom Queries

So this one should seem pretty obvious, but it wasn’t apparent to me at first.  It was only after digging through the wp-db.php file that I discovered how to enable error reporting.

The Problem

As you may have discovered, the wp_query() function isn’t a “one size fit’s all” solution.  Often times, you may need to query the WordPress database using a custom MySQL query.  Especially  if you are using WordPress for anything other than a blog (ie freshapps.com).

When writing custom queries, it can often be frustrating if you make a mistake in the SQL syntax as WordPress will simply display no results.  For example:

$results = $wpdb->get_results("SELECT * FROM $wpdb->posts 
   WHERE post_title = 'foo bar baz");
print_r($results);
 
// Outputs Array ( )

Since we have made an error in our SQL statement (I didn’t add the second single quote), WordPress will suppress it and simply return an empty array. This is not very helpful for debugging.

The Solution

The solution is actually quite simple. The global $wpdb object has a property called show_errors. Setting this property to true will cause WordPress to output the SQL errors to the screen for a given query.

Here it is with our example above

// Enables Wordpress's DB Error reporting
$wpdb->show_errors = true;
 
$results = $wpdb->get_results("SELECT * FROM $wpdb->posts 
   WHERE post_title = 'foo bar baz");
print_r($results);
 
// Outputs 
// WordPress database error: [You have an error in your 
// SQL syntax; check the manual that corresponds to your 
// MySQL server version for the right syntax to use near 
// ''foo bar baz' at line 1]
// SELECT * FROM wp_posts WHERE post_title = 'foo bar baz

Now we know what went wrong with our query rather than just receiving empty results.

Let me know if you have any questions or comments.

Happy WPCoding!

Reblog this post [with Zemanta]

29
Jan 10

Emacs For OSX Is Out!

For all you Vi using, Emacs haters out there, I will fight you!


4
Dec 09

Programming Tip Of The Day #2 – Difference Between i++ and ++i

To some, this should seem a bit obvious and if I am insulting your intelligence by discussing it, I am sorry.  But, one of the main reasons I want to discuss this topic is, I was asked this question in a job interview for Lockheed Martin.

What is the difference between i++ and ++i?

The answer is actually quite simple.

i++ first evaluates the value of i and then increments it

++i increments the value of i and then evaluates it

Here is a brief example to demonstrate what I mean.

// Example: i++ 
$i = 5;
echo "The value of i is " . $i++ ;
// Output "The value of i is 5"
// i = 6
 
// Example: ++i 
$i = 5;
echo "The value of i is " . ++$i;
// Output "The value of i is 6"
// i = 6

So, now if you are ever asked about this in an interview, you will have a response.

Happy programming!


13
Nov 09

Programming

99% of students that were in my CS degree program

99% of students that were in my CS degree program

Image taken from abtrusegoose


10
Aug 09

Displaying Your FeedBurner Subscriber Count Anywhere – PHP Coding Tutorial

feedburner-logo1

If you are a serious blogger (and I’m sure you are), you probably track your RSS subscribers with FeedBurner.  If you don’t, you should be.  One thing that has always bugged me about FeedBurner is if you ever wanted to display the number of subscribers on your blog, you were stuck using their image.  The image looks like this:

I’m sure you have seen this logo everywhere.  Well, not too many people know it, but FeedBurner actually has a very simple API that allows you to just get the subscriber count so you can display it however you would like.

So rather than being limited to this boring icon, you can display your live subscriber count anywhere on your blog.  If you are feeling adventurous, you could even super impose it on to your own custom image using the GD library (tutorial to come if sufficient interest, post in the comments if you would like to see it).

Let’s get started…

1. Activate the Awareness API Inside Of Feedburner

Log in to FeedBurner.  Click the Publicize tab and the click Awareness API.  Finally, click Activate.  The service is now enabled.

awareness_api

2. Write The PHP Code To Interface With FeedBurner

There is a lot of data that you could potentially get from FeedBurner, but the code below will just show you how to get your subscriber count.

All you need to do is make a simple GET request using CURL to the URL https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=brandontreb . Of course replacing brandontreb with your feed’s name.

    <?php
     
    $url	= "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=brandontreb";
    $ch 	= curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch); 
    curl_close($ch);		
    if ($data) {
    	echo $data;
    	preg_match('/circulation=\"([0-9]+)\"/',$data, $matches);
    	if ($matches[1] != 0) {
    		$subscriberCount = $matches[1];
    	}
    }
     
    echo "Join the other $subscriberCount people and subscriber to my RSS feed."
     
    ?>

    This code is pretty straight forward with a little trickiness to parse the XML. We first make a CURL connection to the URL. Just replace brandontreb with the title of your RSS feed inside of FeedBurner.

    Next we print the data. Note: You won’t see the data in your browser unless you do a view source. Since it is XML, your browser will treat it like HTML and not display it. So, we do a preg_match for the element circulation and get it’s value. (Pretty sick right?). The value of our subscriber count will be at index 1 of the matches array.

    Then, we just print the subscriber count and voila!

    Now, you are no longer a slave to that generic FeedBurner subscriber count icon. Be sure to check out their API for other cool things that you can do with their web services. If you have any questions, feel free to leave them in the comments section of this post. Happy Coding!


12
Mar 09

New Web Application That Will Be Useful For iPhone Games Developers

So, after downloading many iPhone games and talking with a ton of iphone developers, I have a found a huge need in the community. Over the next week I will be developing a killer web application with a web service API that will be free to iPhone developers to use.

I am not going to give out any details at the moment, but be sure to subscribe to my RSS feed as I will be blogging about it through out next week (Spring Break = Time to develop). It should be completed by late next week.

I guarantee, if you are an iPhone game developer, you will benefit tremendously from what I am about to release.

Excited? So am I…