MySQL Logo

MySQL Error Reporting In PHP

4th February 2008 - 3 minutes read time

Using MySQL as a database engine in PHP is very powerful, but one thing that can be a pain is trying to debug code. Spotting the difference between a PHP error and a MySQL error can be hard with larger systems.

A good way of debugging MySQL code is by using the mysql_errno() and mysql_error() functions. These functions print off the last error that yuour MySQL server encountered so it can tell you exactly what is wrong with your SQL statements.

The following example code tries to get data from a non-existent table. The $result variable is set to false as the call failed so the error functions print off the SQL error.

Read the full article

PHP Logo

Using PHP Sessions To Detect Returning Users

3rd February 2008 - 3 minutes read time

To detect a user returning to a web page you can use the built in PHP session manager. At the start of the code you can use the session_start() function to initiate the session, you can then use the $_SESSION global array to store and retrieve information. The session_start() function sends a cookie to the client with a unique code that looks like this

8a9af5644326881594811db6fe96faf8

The session variable information is kept in a file on the web server and when the session_start() function is called PHP looks for the file with the corresponding name. It then parses this file and loads the variables into memory. This is all done behind the scenes by PHP, all you need to know is that you can set values in the $_SESSION array and get them back the next time the page is loaded.

Read the full article

PHP Logo

Benchmark PHP Code With microtime()

2nd February 2008 - 3 minutes read time

Sometimes is is necessary to see how long your PHP code runs for. This can be done using the following function and examples. This will convert the result of the php function microtime() into a float value.

function getmicrotime($t){
  list($usec, $sec) = explode(" ",$t);
  return ((float)$usec + (float)$sec);  
}

Use this function to see how long something runs for. At the start of the code call the microtime() function and store the result at the start. At the end store the result of the microtime() function as the end and then use the two values to figure out how long the code took to run.

Read the full article

WordPress Logo

Procedure For Changing The Directory Of A Wordpress Blog

1st February 2008 - 3 minutes read time

For many sites the blog is only part of the site, not the main reason for the site existing. In these cases the blog is kept in the directory /blog or similar. Occasionally (for what ever reason) it might be necessary to change the directory. To that end here is a small walk through of the steps you need to take in order to do this.

Read the full article

PHP Logo

Using PHP To Generate CSS

31st January 2008 - 3 minutes read time

Generating CSS with PHP has several benefits. For example, you can keep all of your colour declarations as PHP variables so if you need to change any colours it only takes a small edit and not a find/replace operation.

Getting PHP to generate CSS requires just two steps. The first thing to do is to open your CSS file and insert the following line at the top. This tells the browser that the file is CSS.

Read the full article

JavaScript Logo

JavaScript Scrolling Box Marquee Replacement

29th January 2008 - 14 minutes read time

Having a Marquee on a web page is a nightmare from an XHTML validation point of view, so here is a neat function that will produce the same effect as a vertical scrolling marquee, that passes XHTML validation, and built entirely from JavaScript and CSS.

Vertical Scrolling

Take the following HTML code. Don't worry about reading it. It is just 7 p tags (2 of which act as spacers) contained within a div tag.

Read the full article

MySQL Logo

Logging Onto A MySQL Database

28th January 2008 - 4 minutes read time

If you have command line access to your MySQL database server you will need to use certain parameters to log in. Most web hosts will not allow you to do this, so you might want to install MySQL into a local computer and give it a go.

To log into mysql you must run the program called mysql with certain parameters. Here is an example.

./mysql -u username

One thing you must realise is that all usernames are associated with a host so if the user you specified can't access the server from this host then you won't get far. To specify the host location enter the -h flag.

./mysql -h hostaddress -u username

If your user is able to access this server then you will be asked for a password. You can set the password in the string using the -p flag. If this doesn't work then leave out the space between the -p and the password.

Read the full article

Flashing JavaScript Text

27th January 2008 - 2 minutes read time

Here is a simple function that makes text in a tag fade into one colour slowly before quickly fading back into the original colour. If the background is the same colour as the text then the text will appear to fade in and out.

Read the full article

PHP Logo

PHP5 Error Reporting

26th January 2008 - 7 minutes read time

PHP has some very nice error reporting features, which can tell you many things about the code that you are trying to execute. This error reporting is always nice to have available when debugging code as it helps you solve many of the common mistakes that occur when creating dynamic web pages.

However, this error reporting is almost always turned off on production servers as it can reveal information about the server that you wouldn’t want everyone to see. For example, the errors can reveal information about server file structure, database fields in queries, database usernames, $_GET and $_POST commands and so on.

Read the full article

WordPress Logo

Enable Custom Field Search In Wordpress

25th January 2008 - 10 minutes read time

When you write a post in Wordpress you can set certain custom fields. The default search behaviour of Wordpress is to search only the title and main text of the posts, which makes these custom fields not all that useful. With a little bit of tinkering you can get Wordpress to search any custom fields that you have set, so if you store things like "Author" you can allow people to view all posts by that author by clicking on a link or doing a search. To see more information about Wordpress custom fields see this Wordpress codex site article. Wordpress stores these custom fields in a table called postmeta where each custom field name (called meta_key) is associated with a custom field value (called meta_value).

Read the full article