Development

Help keep the internet free

mysql_logo

A big part of the Internet is built on LAMP (Linux, Apache, MySQL and PHP/Perl/Python). Now Oracle is trying to buy Sun, which owns MySQL.

It’s not in the Internet users interest that one key piece of the net would be owned by an entity that has more to gain by severely limiting and in the long run even killing it as an open source product than by keeping it alive. If Oracle were allowed to acquire MySQL, we would be looking at less competition among databases, which will mean higher license and support prices. In the end it’s always the consumers and the small businesses that have to pay the bills, in this case to Oracle.

Read on here at Monty’s blog (creator of MySQL) or head here for the official website.

Help keep the internet free Read More »

Using LIMIT when querying a single row

mysql_logo

Sometimes you know you are only looking for one row when you are querying your tables. It may be that you are fetching a unique record or checking the existence of any number of records that satisfy your WHERE clause.

Adding LIMIT 1 to your query can increase performance in this situation; – the database engine will stop scanning for records after it finds just one, instead of going through the whole table or index.

For example:
$record = mysql_query("SELECT username FROM user WHERE country = 'UK'");
if (mysql_num_rows($record) > 0) {
// ...
}

Can become:
$record = mysql_query("SELECT username FROM user WHERE country = 'UK' LIMIT 1");
if (mysql_num_rows($record) > 0) {
// ...
}

Using LIMIT when querying a single row Read More »

Suversion Cheat sheet

Subversion

A great Subversion cheat sheet is available from http://ariejan.net/

For those of you not yet in the know, Subversion (SVN) is a version control system initiated in 1999 by CollabNet Inc. It is used to maintain current and historical versions of files such as source code, web pages, and documentation. Its goal is to be a mostly-compatible successor to the widely used Concurrent Versions System (CVS).

Get the cheatsheet here

A few other useful links:

Tortise SVN client (Windows only)
Workbench SVN client
Subclipse SVN client
Ankhsvn (Plugin for Visual Studio)
WebSVN (php based repository browser)

Suversion Cheat sheet Read More »

Dont copy variables without reason

php-logo

Sometimes PHP novices attempt to make cleaner or more legible code by copying predefined variables to variables with shortened names prior to working with them. This actually results in doubled memory consumption (when the variable is altered) and therefore slower scripts. In the following example, if a user had inserted 512KB worth of characters into a textarea field this would result in nearly 1MB of memory being used.


$title = strip_tags($_POST['title']);
echo $title;

This operation can be performed inline, – avoiding the memory overhead.


echo strip_tags($_POST['title']);

Dont copy variables without reason Read More »

PHP Twitter Class

php-logo

Twitter is a service for friends, family, and co-workers to communicate and stay connected through the exchange of quick, frequent answers to one simple question: What are you doing?

PHP Twitter is a (wrapper)class to communicate with the Twitter API written by Tijs Verkoyen. Download page is available here

A quote from the author (and he’s not wrong) The class is well documented inline. If you use a decent IDE you’ll see that each method is documented with PHPDoc. There is also a tutorial available here

So, to sum up, these links should kickstart your twittering ability from PHP (and your website).

Happy tweeting!

PHP Twitter Class Read More »

The future of PHP

php-logo

A few changes in PHP 6:

This post is mainly about what is removed (and will no doubt break countless scripts out there)

  • magic quotes
  • register_globals
  • register_long_arrays
  • safe_mode

magic_quotes


// Assuming magic_quotes is on
$sql = "INSERT INTO USERS (USERNAME) VALUES $_GET['username'];

// Using proper parameterised query method (MySQL)
$statement = $dbh->prepare("INSERT INTO USERS (USERNAME) VALUES ?";
$statement->execute(array($_GET['username']));

Obviously the get_magic_quotes_gpc() function will no longer be available.

register_globals

// a security hole because if register_globals is on the value for user_authorised can be set by a user sending
// them in the query string
// i.e www.example.com/index.php?user_authorised=true
if ($user_authorised){
// show all the data
}

// Being specifc
function is_authorised{
if (isset($_SESSION['user'])){
return true;
}else{
return false;
}
}
$user_authorised = is_authorised();

register_long_arrays

Using deprecated registered arrays:

// Echo the name of the user value given on the query string
// http://www.example.com/index.php?username=notgood
echo "Welcome, $HTTP_GET_VARS['username']";

Using $_GET

// Using the supported $_GET array instead
echo "Welcom, $_GET['username']@;

safe_mode
This was originally to ensure that the owner of a file being operated on matches the owner of the script that is executing. It was originally a way to attempt to handle security when operating on a shared server environment (like many ISPs would have) It is outside the scope of this blog to document the numerous functions affected by this change, so consult your documentation.

The future of PHP Read More »

Maxlength for MySQL TEXT field types

mysql_logo

MySQL supports four TEXT field types (TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT)

MyISAM tables in MySQL have a maximum row size 65,535 bytes and all the data in a row must fit within that limit.

Luckily however TEXT field types are stored outside of the table itself and thus only contribute 9 – 12 bytes towards that limit.

Further reading is here.

Because TEXT data types are able to store so much more data than VARCHAR and CHAR field types it makes sense to use them when storing web pages or similar content in the database.

The maximum amount of data that can be stored for each data type is approximately:

TINYTEXT 256 bytes
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB

So most of the time TEXT will suffice, but if you are scratch building a CMS it might be an idea to think about MEDIUMTEXT

Update: (20/05/2014) – I see a lot of hits on this page, so I thought I’d spell out the information here for the terms you seem to be searching for…

TINYTEXT is a string data type that can store up to to 255 characters.

TEXT is a string data type that can store up to 65,535 characters. TEXT is commonly used for storing blocks of text such as the body of an article.

MEDIUMTEXT is a string data type with a maximum length of 16,777,215 characters. Use MEDIUMTEXT if you need to store large blocks of text, such as a book.

Maxlength for MySQL TEXT field types Read More »

Understanding CSS Class and ID

css_logo

Often these selectors can confuse beginners. In CSS a class is represented by a dot “.” while an id is a hash “#”. Simply put an id is used on a unique style that doesnt repeat whilst a class can be re-used.

Often it can be hard to decide where to use a class versus an id for an element

Use a class tag if:

1.The style is used in various places throughout the document.
2.The style is very general.

Use an id tag if:

1.The style is only used once ever in the document.
2.The style is specific to a certain area of the document.

Remember that an id can only appear once in any HTML document. Once you’ve used that id it should not be used again on that page.

Understanding CSS Class and ID Read More »