Friday, November 06, 2009

Checking for empty variables in PHP

While writing some php code I checked for an empty string, using the empty() function, but I got this error :

PHP Fatal error:  Can't use method return value in write context
I found this very strange because I've used the empty() function many times before. It turns out that the empty() function can not handle the return value of a function or method. You can only check variables using this function.

The solution is simple, store the return value of a function in a variable, and then check the variable with the empty() function :

wrong
if ( empty ( some_function() ) ) {

right
$some_variable = some_function();
if ( empty ( $some_variable ) ) {



On a side note, using the empty() function, is the best way to check for an empty variable, because it combines multiple checks, and works for different variable types. See documentation for details. When relying solely on strlen() or isset(), you can get an unexpected result.

Saturday, October 03, 2009

New challenge

About a year ago I started something that, a few days ago, came to a conclusion - but to be fair, it's more a milestone than a conclusion.
In August of last year (2008), I applied for a job at Ghent University. In the months that followed I went through the selection process, and after some hurdles I got selected. In April of this year (2009), I started on my new job. And now, six months later, my trial period is over. After a very good evaluation, I was appointed to the job, starting October 1st.
It took me over a year to get so far, but now I've reached what I was aiming for since then. But of course, this is just the start of it all. I have the rest of my life's working career ahead of me at Ghent University. And I'm still getting started, with a lot of plans to accomplish, projects to start, task being assigned, ... I'll know what to do the next few years.

But now that I've reached some stability on the work front, I might think of a new challenge, some new projects to start, or some overall reorganisation of the goals and priorities in my life. I've been thinking of a few, some silly or ridiculous, others infeasible (at least on my own), or down right impossible ways to pass time:


  • Find a way of solving any Rubik's cube in maximum 21 steps. The current proof says maximum 22 steps are needed.

  • Write a socio-philosophical essay addressing some effects of globalism : economy, environment, politics, communication, pluralism, culture, migration.

  • Now that, Gianfranco Gentile, a researcher of Ghent University has discovered that dark matter is needed to explain certain behavior in galaxies, it might be time to create a theory combining classic, relativistic and quantum mechanics together with dark matter.

  • Work on something revolutionary to save energy, use available energy more efficiently, not only by technology, but also by a change of mentality.

  • Build a render farm.

  • Start beatboxing.

  • Research methods of efficiently stacking solids, and by extension, deformable solids (f.e. a sealed bag containing a liquid).


But some are more realistic so I'll focus on those :

  • Learn to solve a Rubik's Cube. This is trivial, but I would like to find it by myself.

  • Pick up playing the piano again.

  • Read more, by which I mean : books, literature.


Or I could just try to look for goals in my work, find a girlfriend, buy a house, get married, start a family and raise some children.

Wednesday, March 11, 2009

Rename multiple tables in MySQL database

Having to rename multiple tables with a similar pattern (prefix, suffix, ...) by replacing the recurring pattern, isn't an easy task, because there is no simple function or statement for doing it, not as far as I know.
Renaming tables like 'table_1234' and 'table_3456' to 'new_table_1234' and 'new_table_3456', is pretty straightforward in SQL:


RENAME TABLE `database`.`table_1234`
TO `database`.`new_table_1234`;
RENAME TABLE `database`.`table_3456`
TO `database`.`new_table_3456`;

or in one SQL statement :

RENAME TABLE
`database`.`table_1234` TO `database`.`new_table_1234`,
`database`.`table_3456` TO `database`.`new_table_3456`;

But when renaming a lot of tables, this method becomes tedious and time consuming.

This PHP script automates the renaming of multiple tables in a MySQL database. It lists all tables in a MySQL database, which contain a defined string pattern. The script creates and executes a series of SQL statements, which rename the table by replacing the search pattern in the original table name with another pattern in the new table name.

This script can easily be modified to rename multiple databases, or when stripping the original pattern (f.e. a prefix) and adding a new pattern (f.e. a suffix) is needed.

<?php
$db_server = "localhost"; // hostname MySQL server
$db_username = "username"; // username MySQL server
$db_password = "password"; // password MySQL server
$db_name = "database"; // database name

$pattern = "pattern_"; // search string
$new_pattern = "new_pattern_"; // replacement string,
// can be empty

// login to MySQL server
$link = mysql_connect( $db_server, $db_username, $db_password);

if (!$link)
{
die('Could not connect: ' . mysql_error());
}

// list all tables in the database containing the search pattern
$sql = "SHOW TABLES FROM `" . $db_name . "`";
$sql .= " LIKE '%" . $pattern . "%'";

$result = mysql_query ( $sql, $link );
if (!$result)
{
die("Invalid query: " . mysql_error( $link ));
}

$renamed = 0;
$failed = 0;

while ( $row = mysql_fetch_array ($result) )
{
// rename every table by replacing the search pattern
// with a new pattern
$table_name = $row[0];
$new_table_name = str_replace ( $pattern, $new_pattern, $table_name);

$sql = "RENAME TABLE `" . $db_name . "`.`" . $table_name . "`";
$sql .= " TO `" . $db_name . "`.`" . $new_table_name . "`";

$result_rename = mysql_query ( $sql, $link );
if ($result_rename)
{
echo "Table `" . $table_name . "` renamed to :`";
echo $new_table_name . "`.\n";
$renamed++;
}
else
{
// notify when the renaming failed and show reason why
echo "Renaming of table `" . $table_name . "` has failed: ";
echo mysql_error( $link ) . "\n";
$failed++;
}
}

echo $renamed . " tables were renamed, " . $failed . " failed.\n";

// close connection to MySQL server
mysql_close( $link );
?>

Syntax Highlighting in Blogger

I've added some Javascript to the template of my blog to get Syntax Highlighting working. This means source code is formatted so it is easier to read. If it works allright, you should see an example of some highlighted php-code below. (The source code is not formatted/coloured when reading this post through an RSS-feed.)


<?php
/* Sample php code
This code doesn't do anything at all.
It just contains some php elements that should be formatted
and coloured.
*/

$some_variable = "some_string";

// Some comment
$result = some_function( $some_variable );

if (!$result)
{
die("It doesn't work!");
}

$some_array = new array();

$some_array[0] = $result;
?>

Saturday, February 07, 2009

Green Plug Universal Power Adaptor

How many electronic devices do you have? A laptop, cell phone, MP3 player, external hard disc, GPS, ...
And each of them has its own adaptor, which can only be used with a specific device and isn't compatible with your other devices.
So you end up with several different adapters powering your devices, which results in a lot of cables that can get entangled, and which take up a lot of power plugs.
And if an adapter breaks, you have to buy a spare one from the original manufacturer, which costs a lot.

Wouldn't it be nice if there was a universal adapter for all your electronic devices?
You would only have to take one adaptor with you, to charge your cell phone, MP3 player and laptop. If a friend or colleague forgot to take her or his adapter, he or she could easily use yours, even if he or she uses a laptop of a different brand.

Actually, there is an initiative to standardize power adaptors :
Green Plug Universal Power Adaptor.


GreenPlug Universal Adapter

Monday, January 12, 2009

Enabling wireless network on an Acer Aspire One in Ubuntu

I just installed Kubuntu 8.10 on an Acer Aspire One. Everything seems to work fine, but the wireless network doesn't seem to work.
Luckily I found this HOWTO on enabling the wifi.

Friday, November 21, 2008

Social Web

Social networking websites are becoming more and more popular, and it's number is growing. Some websites provide a unique service, while other websites seem to be more of the same.
Some of your friends join one website while others join another, and both of them are requesting to join them on their website of choice.

After a while you have an account with several of those websites, and you start to notice you are providing the same information over and over again, on every of those websites : personal details, interests, schools you attended, employers you worked for.
And you have to (re)connect to your friends on every of those websites.
Not to mention when something changes, for instance when you change jobs, you have to change it on every website and hope you don't forget any of them.

Wouldn't it be convenient if all of the information you provide on those websites, can be shared between those websites? If you want to change something, you can do it in one place and all other social networking websites where you have an account adopt these changes automatically? If you add a friend on one website, and that friend has an account on another website where you have an account too, that this friend is added there as well?

I'm not the only one asking the same questions. And it seems some effort is put into creating a Social Web, where this kind of (personal) data can be shared between (social networking) websites in a open, easy and secure way.
These are a few of the initiatives :

A recent presentation (June 2008) was held with an overview of these efforts and what the future will bring. Unfortunately, these efforts, for now, are quite theoretical and about creating standards based upon existing web standards. Several protocols are proposed (FOAF, XFN, hCard, GRDDL, RDF, OpenID, OAuth, REST, ...) to be used in this new standards.

No real usable application is available at the moment, not as far as I know, but the big players (MySpace, Facebook, Google, Twitter, Netlog, ...) are backing some of these initiatives.