Everything PHP: MySQL Resources

Published on Aug 21, 2009   //  Development

Everything PHP

When you execute a query on a MySQL database through PHP, a resource is returned back to you. This resource contains the information that you queried from the database. However, a resource is not a data type we are used to working with, nor can we work with it directly like an array.

So, we’ll need to use other functions as sort of a bridge between a resource and something we can readily work with (an array). Today, we’ll be looking at a function that will allow you to get the data you retrieved from the database into an associative array. This is the function I use most often when processing database querys.

The function in question in mysql_fetch_assoc(). This function will convert the resource into an associative array, where the field name is the key, and the value of the field is the value of the array. Let’s take a look at an example of how this function works:

[php]$query = mysql_query("SELECT [field_names] FROM [table_name]");
$query_array = mysql_fetch_assoc($query);
print_r($query_array);[/php]

However, this function may not work as you would expect. This function will create an associative array from one of the rows from the given resource, it will then move the (internal) row data pointer forward to the next. So, to get the associate arrays of all the returned rows, we need to use this function in a loop.

As this function will return false once all the rows have been exhausted, the best loop to use would be while. Which you would use in a similar fashion to this:

[php]$query = mysql_query("SELECT [field_names] FROM [table_name]");
while ($query_array = mysql_fetch_assoc($query)) {
print_r($query_array);
}[/php]

You can now iterate through your MySQL resource and process the data how you need to.

Web Development: Suggestions

Published on Aug 18, 2009   //  Development

Web Development

Over the last eight months of this Web Development series, we have covered quite a bit of material. XHTML, CSS and Javascript, which form the basics of the web. I am now opening the virtual floor up to suggestions for what we should cover next.

Do you want to see more in-depth examples of what we covered? Learn a little about XML? Something else? Leave your suggestions in the comments below, and we’ll see about writing about the topics that you want to learn about.

Moving Beyond SSH: Creating Archives

Published on Aug 16, 2009   //  Development
Off

Moving Beyond SSH

Just like with the operating system running on your home computer, your server can create archives (“zip” files, compress files, to put it differently). While creating archives through SSH isn’t as straight forward as with your own computer, it is possible. We’ll be going over how to create three types of archives today: tar (.tar), gzipped tar (.tar.gz) and zips (.zip).

Tar

A tar archive is an archive that compresses all files included as a single file.

tar -cf archive.tar list of files separated by spaces

Gzipped Tar

A gzipped tar is a tar archive that has also been run through gzip to compress it further. This results in a smaller filesize.

tar-cfz archive.tar.gz list of files separated by spaces

Zip

A zip archive is the most popular (and widely support) archive type. With a zip archive, each file included in compressed separately. In theory, this would provide better compression due to the varying types of files that may be included. In practice, this zip archives are usually larger than their tar equivalents.

zip archive.zip list of files separated by spaces

WordPress Development: Preview Image

Published on Aug 15, 2009   //  Development

WordPress Development

When your theme is displayed in the Appearance section of the WordPress administrator interface, it is possible for a small screenshot to be displayed along with it. This allows users to see a quick preview of what your theme looks like with activating it or, in recent versions of WordPress, clicking the Preview button. The screenshot will also allow users to quickly differentiate it from the other themes listed in their Appearance section. If you’re planning on making the theme you’re creating public, then you should defiantly include a screenshot.

To include a screenshot in your theme, you first make take a screenshot of what your theme looks like. Methods on how to do this will vary depending on your operating system and image editing software available to you. Begin by taking a screenshot of your theme loaded in a browser. Next, load this into an image editor (anything capable of cropping and resizing will work) and crop out the browser frame in the screenshot. Then, resize your screenshot to 300×225 pixels (you may need to do some more cropping to get the ratio correct).

Once you have the screenshot resized, save it as screenshot.png in the root of your theme folder (where your style.css file is located). Then, you’ll be able to view your screenshot in the Appearance section. It’s that simple.

Everything PHP: Queries

Published on Aug 13, 2009   //  Development

Everything PHP

We have now gone over how to connect to a database using PHP, as well as some of the essential SQL queries. Finally we can put these two lessons together and query the database within PHP. Executing queries in PHP versus directly to MySQL is more or less the same.

Before we can begin executing MySQL queries via PHP, we need to connect to MySQL and select our database. Please see that article for details on how to do that, as we are just going to be going over the querying part today.

To execute MySQL queries, we’ll be using the PHP function mysql_query(). Just as the name implies, this functions allows you to send SQL queries to the connected MySQL database. Let’s take a look at a basic example:

[php]mysql_query("SELECT [field_names] FROM [table_name]");[/php]

We encase the SQL query in double quotes (as opposed to single quotes) because we will need to use single quotes to wrap around values. Otherwise we would have to escape the single quotes each time, which is far from ideal. There is a glaring problem with the above code. If you were to make an error in your query, the function will not output an error (the function will return false), and any code relying on this query will not work correctly (which could be hard to debug).

The solution to this is to ensure that if mysql_query() returns false we stop the execution of the rest of the file, and output the error MySQL returned. We can use an or exit() statement and use the mysql_error() function to get the last error MySQL reported. Let’s look at the above example with this error handling added:

[php]mysql_query("SELECT [field_names] FROM [table_name]") or exit(‘A MySQL error has occurred: ‘ . mysql_error());[/php]

A quick note on exit(): The functions exit() and die() are equivalent and there’s no standard on which to use. As such, you’ll may see a different one than you are used to in other’s programming. Just wanted you to be aware.

If you which to use values (such as in a where clause), they must be encased in single quotes, even when you’re using a variable as the value.

When the MySQL query is successful, the function will return a resource. You’ll need to use some other functions to be able to handle and process a resource. Which is what we’ll be covering next week.

WordPress Development: IRC Development Chats

Published on Aug 6, 2009   //  Development

WordPress Development

Taking a detour from our posts about theme development for this week, we’ll be talking about an aspect of the “core” development of WordPress itself. If you’re developing themes and plugins for WordPress, you may also want to consider helping developing WordPress itself. WordPress is open-source, and, as such, anybody can contribute code to it.

A good way to follow along (and have a chance to discuss) with the development of WordPress is to join the weekly IRC chats. During these IRC discussions, “core developers” (those who have the ability to commit changes to WordPress) and community developers (those who contribute code, without having commit access to WordPress) will discuss the development of the next version of WordPress.

A rough agenda for the chat is usually created a week before the chat. You can participate in suggesting items for the agenda here. These IRC chats happen every Thursday at 9PM UTC during the active development stages for WordPress. You can join in on these chats on the #wordpress-dev channel on irc.freenode.com using an IRC client.

If you’re interesting in the core development of WordPress, join in on these chats and voice your opinion (or just sit back and follow along silently, if you prefer).

Page 5 of 42« First...34567...102030...Last »