Thursday, March 22, 2007

How to query MySQL from PHP correctly

<?php
/*
1. Connect to the database
2. Prepare statement
3. Process results
4. Close statement
5. Close connection
*/

//Create database connection
$mysqli = new mysqli("localhost", "myuser", "mypass", "mydb");

//Check for connection errors
if (mysqli_connect_errno()) {
   //It's not always a good idea to show error text to the user
   printf("MySQLi connection error: %s\n", mysqli_connect_error());
   exit();
}

//Create prepared statement
if ($stmt = $mysqli->prepare('SELECT `uName` FROM `myusers` WHERE `uId` = ?') {

  //Statement is prepared, bind a variable to the query
  $stmt->bind_param("d", $varUserId);

  if ($stmt->execute()){

    //Query executed successfully so bind results to variables
    $stmt->bind_result($uName);

    while ($stmt->fetch()) {
      //Fetch results
      echo $uName.'<br />';
    }

  } else {
    //Execute statement issued an error - for example server closed the
    //connection unexpectedly
    //Again, it's not always a good idea to show error text to the user
    printf("MySQLi error: %s\n", $mysqli->error);
  }

  //Remember to always close the statements or you can have problems later
  //This method can also fail (but it would be too formal to treat this kind of errors)
  $stmt->close();

} else {

  //Prepared statement issued an error - usually query parse errors
  //Again, it's not always a good idea to show error text to the user
  printf("MySQLi error: %s\n", $mysqli->error);

}

//Close connection - it's not a must but it's a good habit
$mysqli->close();

?>

Monday, March 19, 2007

A new way to learn HTML : IE7 errors

Geek language is very common in error messages because using it we can understand the source of the errors better. But I think they exagerated in this case without any reason. How about using a simple description like this next time: "502 Bad Gateway: The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request."

Saturday, March 17, 2007

Sitemaps - make Googlebot aware you have something for it

"The Sitemap Protocol is a dialect of XML for summarizing Sitemap information that is relevant to web crawlers. For each URL, you can include crawl "hints" like the last modified date and approximate change frequency."

There are many SEO techniques you can play with but this one has some important advantages:

  • it's easy to implement
  • shows results in a very short time
  • it's recommended and offered by Google so you won't get blacklisted
The reason you would want to use a Sitemap is that I'm sure you know your website better that Googlebot does. So why not sharing the knowledge and let it know what you think is important and what is not. I used to underestimate the power of sitemaps until I saw this (Googlebot activity spike) a week after creating the Sitemap for my website:



The first time I tried to use sitemaps I got lost in the protocol specifications that Google provides, the fact that Google Sitemap Generator is not as user-friendly as I expected and in the multitude of options you have for submitting your sitemap. Well, it seems that the whole process is simpler that I thought. Here are the only steps you have to follow:

1. Create or generate the sitemap
2. Make sure it is valid
3. Submit it
4. Update it

1. How to create a sitemap?
The following image shows a Sitemap in XML format. The Sitemap in the example contains two URLs, each of which is identified using the XML tag. In this example, a different set of optional parameters has been provided for each URL.


Due the fact that XML files are just text files you can create them by hand or, if you have a more then a dozen URLs, you can generate them using PHP. So, go write a simple "sitemap.xml" file. Even a small sitemap will help. Repeat the <url>...</url> block for each link you want to include and don't forget to close the <urlset> tag at the end of the file.

2. If it's not valid it has no value
The next step is validating your file. And this is a must because XML parsers are less error tolerant than HTML parsers. So if your file has errors it will not be processed. There are plenty of ways to validate your file but the most easy I found is using validome.org. Just upload it to your website and point the validator to it.


3. Let Google know you have a sitemap (or more)
Google has a service called Google Webmaster tools which we will use to upload our sitemap. After you log in (you will need a Google account for that) just go to the Sitemaps section and click "Add a sitemap". Hopefully, if everything is ok you will see something like this after a while:


4. Keep your sitemaps up to date
Your Sitemap is more valuable if it's up to date. That is it points to working URLs and parameters reflect the state of the links. So, don't forget to update them from time to time. This is it, good luck!



Sunday, March 11, 2007

Worst ISP in the world

Location: Romania, Cluj Npoca, AMNET "infrastructures". This can be unbelievable for some but this so called ISP existed as a company and had clients. Original images were posted by another ISP in the same city but they may remove them so here there are (click on the images for large size version):




After their clients noticed the close connection between weather and their internet speed the company quickly went offline. Digg up for the "guerilla" ISP

Thursday, March 8, 2007

Why is DIGG loading faster lately?

Well it seems they finally followed user advices and enabled gzip compression for the pages. If you are on a slow connection you will clearly see the benefit of this action. There is however a lot of room for improvements, see a detailed report in the image below (click to enlarge).

And how the pages are loaded on a 2Mbit internet connection.
and for Reddit...

Saturday, March 3, 2007

How "Web 2.0" is your website?

Today I found a nice way to test my websites and make them more "Web 2.0" compatible :). The original Web 2.0 sense was formulated by O'Relly in 2005 (see the image below). If you wonder if your website is Web 2.0 compatible or not, here are some of the questions you should ask yourself: Is it in public beta? Uses inline AJAX ? Appears to be Web 3.0 ? Attempts to be XHTML Strict ? Mentions Less is More ? Uses RSS ? Mentions Ruby? Mentions Wisdom Of Crowds ? References Firefox?

Some results for popular websites: