Webforumz Newsletter - February 2008
Tips & Tricks
- Stop MySQL displaying Horrible Error Messages
- Use Shorthand CSS to lighten file size and load time
- Test PHP and MySQL on your local server
Stop MySQL displaying Horrible Error Messages
There is a very easy solution to this problem. Obviously, the connection should never go wrong, but we all know that a simple typo can creep in and suddenly you are left with a website displaying horrible errors, which is a large turn off to visitors, and/or potential guests.
The trick is to place '@' in front of the mysql_query. For example:
$sql = mysql_query("SELECT id, text, author FROM table");
Becomes:
$sql = @mysql_query("SELECT id, text, author FROM table");
The '@' simply tells the database that, if there is a failure, to do nothing. However, the chances are you will still want an error message, so we can add in an 'if' statement:
$sql = @mysql_query("SELECT id, text, author FROM table");
if(!$sql) {
exit('
Error fetching data
');
}
One final trick is the use of 'exit'. This tells the code to not 'use' any PHP after that line. So, any PHP below the above, will not be used if there is an error fetching the data.
Use Shorthand CSS to lighten file size and load time.
Currently you might be styling margins like so:
margin-top: 50px;
margin-right: 10px;
margin-left: 15px;
margin-bottom: 20px;
But there is an easier way. You can combine all those styles together into one declaration:
margin: 50px 10px 20px 15px;
That will assign all your margins. The order is: margin: Top Right Bottom Left;
Incase you have trouble, coder Alex Perry came up with this handy way of remembering!
The
Rhino
Broke
Loose
Or you can assign just two values:
margin: 50px 20px;
This will asign the Top and Bottom margins 50px, and the Left and Right margins 20px. Another useful shorthand is the background declaration. Instead of this:
background-color: #0099CC;
background-image: url'(path/to/your/image.jpg');
background-position: top left;
background-repeat: none;
You can place it, like we did with the margins, into just one declaration:
background: #0099CC url('path/to/your/image.jpg') no-repeat top left;
This shorthand can be done with any style that has sub-values. Two of the best examples include fonts:
font: italic bold 12px Arial;
And borders:
border: 2px solid #0099CC;
Using shorthand can be very helpful to you. It means your CSS stylesheet is not as long, so less scrolling and searching for certain properties. It also can help to decrease the file size of the CSS sheet. This therefore means it takes less time to load, which can only be beneficial to your website.
Test PHP and MySQL on your local server
If you want to test PHP scripts, you would require a web host which supports PHP and, probably, a MySQL database. This can be very restricting, as if you only want to try PHP, not use it regularly, you will not want to spend money on hosting. Or, if you are younger and not able to purchase any hosting, this is also problamatic.
Why would you want to install it on a localhost? Here are some reasons:
- Creating your own Wordpress/Joomla/Etc theme.
- Learning PHP/MySQL.
- Creating your own PHP applications.
- Testing programs, EG: Wordpress, Joomla.

