PHP And MSSQL

This article will show you how to connect to Microsoft’s MSSQL database via PHP. Typically, most PHP configurations involves the use of MySQL database, but every once in a while, there might be a need to connect PHP with MSSQL. So, here’s a sample script to get you started.

<?php
$sql_server = "localhost";
$sql_user   = "username";
$sql_pass   = "password";
$sql_db     = "database"; 
 
// Connect to the database
$db = mssql_connect($sql_server, $sql_user, $sql_pass)
or die("Can't connect to the MSSQL Server."); 
 
// Select a database
$select = mssql_select_db($sql_db, $db)
or die("Can't open the database " .$sql_db); 
 
// SQL statement
$query = "SELECT * FROM tablename WHERE id='3'";
 
// Execute the SQL query
$result = mssql_query($query);
 
// Display rows returned
$num_rows = mssql_num_rows($result); 
echo $num_rows." rows"; 
 
// Display results 
while($row = mssql_fetch_array($result)) {
  echo $row["id"];
  echo $row["first"];
}
 
// Close DB connection
mssql_close($db);
?>

Fix Virtualbox After Kernel Upgrade

Ubuntu 11.04 recently upgraded to Linux kernel 2.6.38-11. Unfortunately, every new Linux kernel introduced on your system will break your Virtualbox setup. This article will show you how to fix Virtualbox with a new kernel. The error will appear if you try to launch a Virtual Machine. You will most likely get the following errors:

As detailed in the error box, you will need to run vboxdrv setup to fix the problem. All you need to do is open up the Terminal and type this command:

$ sudo /etc/init.d/vboxdrv setup

As displayed in the Terminal, the vboxdrv setup will stop the current Virtualbox kernel module, uninstall it, register a new kernel module, and finally start it. This completes the Virtualbox upgrade that’s necessary after each Linux kernel upgrade.

So, in the future, if your Ubuntu distro upgrades to a newer Linux kernel, you know exactly what to do to make your Virtualbox work with the latest kernel.

Validate Email Addresses

Here’s one way to validate email addresses using the regular expression I found online that works great for me. You can use this little piece of PHP code in your forms or just about any place you need it. The regular expression should be typed in one continuous line, but I added a couple of line breaks for legibility purposes, in this example. You can also place this code  inside a function, so you can use it repeatedly by simply calling the function, in this case, validate_email(). If you have a better regular expression for validating email addresses, please share.

The Code

if (!eregi("
  ^[_a-z0-9-]+(\.[_a-z0-9-]+)*
  @[a-z0-9-]+(\.[a-z0-9-]+)*
  (\.[a-z]{2,3})$", $email)):
 echo "Invalid email.";
else:
 echo "Valid Email.";
endif;

The Function

function validate_email($email) {
 if (!eregi("
   ^[_a-z0-9-]+(\.[_a-z0-9-]+)*
   @[a-z0-9-]+(\.[a-z0-9-]+)*
   (\.[a-z]{2,3})$", $email)):
  echo "Invalid email.";
 else:
  echo "Valid Email.";
 endif;
}

Add Tweet Box To Your Blog

Tweet Box allows Twitter users to tweet directly from your website or your blog. To add Tweet Box to your website or blog, simply add this little piece of code to your WordPress single.php theme file. You can place it anywhere you like. I have it installed at the bottom of this single.php page. See below.

<div id="tbox"></div>
<script type="text/javascript">
  twttr.anywhere(function (T) {
    T("#tbox").tweetBox();
  });
</script>

You can modify Tweet Box by passing parameters to the script.

<div id="tweet-box"><h3>Twitter</h3></div>
<script type="text/javascript">
    twttr.anywhere(function (T) {
      T("#tweet-box").tweetBox({
        'counter' : true,
        'height'  : 50,
        'width'   : 440,
        'label'   : "I made it easier for you to tweet this:",
        'defaultContent' : "Just read: <?php the_title(); ?>
        @ulyssesonline",
      });
    });
</script>

In this example, I’m displaying the counter, which counts the number of characters left in the tweet box. You can set the height and the width of the Tweet Box. I have it set to 440px x 50px to match the content area of my blog. I’m also displaying the label, as well as the default content. The default content includes the title of the blog. I’m calling the WordPress’s the_title() function to display the post title.

I hope you find this helpful. Send a tweet.

Canon MX330 Simple Scan

I finally got my Canon MX330 printer to work with Ubuntu’s Simple Scan. I never had a problem with setting up my printer and/or printing documents on Ubuntu, but I never got the scanner to work. Not once, but now it works, with an application called Simple Scan. I’m so stoked! Ok, here’s how I did it. If you have a Canon MX330 and you want Ubuntu’s Simple Scan to work with your printer/scanner, just try to do the following steps.

  1. Download the Canon MX330 ScanGear Drivers.
  2. Unzip the package. Go to the Packages folder.
  3. Type the following commands from the Terminal.
  4. sudo dpkg -i –force-architecture scangearmp-common_3.00-1_i386.deb
    sudo dpkg -i –force-architecture scangearmp-mx330series_3.10-1_i386.deb
  5. The package above is the current version. You may get a newer version.
  6. One more thing, that’s a double dash before force-architecture.
  7. Start Simple Scan from Applications > Graphics > Simple Scan menu.
  8. The application should start scanning.
  9. Save the image as JPG.
  10. That’s it.

Hopefully that setup works for you!

Re-Install Virtualbox VMs

I recently had to re-install my Ubuntu desktop due to issues with pulling back from using Gnome 3. I was getting dependencies errors was when I tried to run “apt-get” from the Terminal. I tried fixing it for half an hour, but I decided to go for a clean install instead to avoid wasting anymore time. Anyways, I have 5 virtual machines that I wanted to save and re-install on a new clean install of Ubuntu. Here are the steps that I took to backup and re-install Virtualbox VMs on a new clean machine.

  1. Back up the “Virtualbox VMs” folder to a USB drive.
  2. Re-install Ubuntu on the same machine or on another.
  3. After the clean install, copy back “Virtualbox VMs” folder to user home.
  4. Then, install Virtualbox.
  5. Start Virtualbox.
  6. Add VMs by invoking Machine > Add.
  7. Point it to corresponding .vdi file.
  8. Repeat until all VMs are re-installed.
  9. Run the VM to validate if successful.
Pretty simple.

Twitter and T.CO Shortener

If you noticed Twitter lately, that most of the short links are being replaced by Twitter’s own t.co. If you happen to tweet a shortened link using one of the lesser known URL shorteners like mine, uly.me, it will be rewritten by Twitter. It’s quite annoying if you ask me, mainly because you lose part of your own identity when you tweet. Anyways, the reasoning behind Twitter’s action is really about control, analytics and security. I get all that, but I prefer to use my own link. Dammit. Anyways. you can read more about Twitter’s action here.

Ubuntu 11.10 New Features

Softpedia just ran an article listing the Top 10 Features of Ubuntu 11.10. Ubuntu 11.10 code name is Oneiric Ocelot. The new features are: a new login screen, improvements to the Unity Desktop, Deja Dup backup utility, Mozilla Thunderbird 7, Firefox 7, new Alt-Tab utility, Libre Office 3.4, Nautilus 3, and a redesigned Ubuntu Software Center. View the article and screenshots here.

Unlocker Comes To The Rescue

I recently had to access my Windows XP partition, which at this point in time, happens very rarely. Anyways, while browsing around the file system, I noticed two odd directories that look suspiciously out of place. I say delete them! Unfortunately, Windows XP won’t allow me delete these two directories. It says I have no permission, even though I am the administrator. I even booted into Safe Mode with Command Prompt. It simply won’t allow me to delete files. What’s up with that? It’s annoying to say the least. So, I downloaded Unlocker after a quick search online. When you launch Unlocker, it will give you the file directory structure. All you have to do is select a directory or file, and then choose the action to take. In my case, I want to delete. Well, after about a minute or two, the directories were gone. Awesome program for deleting directories and files that simply refuse to be deleted.

Unlocker will help you delete files that have with error messages like:

  • Cannot delete file: Access is denied
  • There has been a sharing violation
  • The source or destination file may be in use
  • The file is in use by another program or user

Just sharing.

Linux is 20 Years Old Today

I can’t believe Linux has been around for 20 years. And it started out as a hobby, and it grew into what it is today. It’s quite an amazing transformation of something that’s being offered for free. You can read more about the history of Linux from Ars Technica’s article entitled March of the Penguin: Ars looks back at 20 years of Linux. Here’s the original message from Linus Torvalds when he posted his first message about Linux to the minix newsgroup back in August 25, 1991. The archive is courtesy of Carnegie Mellon.

From: torvalds@klaava.Helsinki.FI (Linus Benedict Torvalds)
Newsgroups: comp.os.minix
Subject: What would you like to see most in minix?
Summary: small poll for my new operating system
Message-ID:
Date: 25 Aug 91 20:57:08 GMT
Organization: University of Helsinki

Hello everybody out there using minix –

I’m doing a (free) operating system (just a hobby, won’t be big and
professional like gnu) for 386(486) AT clones. This has been brewing
since april, and is starting to get ready. I’d like any feedback on
things people like/dislike in minix, as my OS resembles it somewhat
(same physical layout of the file-system (due to practical reasons)
among other things).

I’ve currently ported bash(1.08) and gcc(1.40), and things seem to work.
This implies that I’ll get something practical within a few months, and
I’d like to know what features most people would want. Any suggestions
are welcome, but I won’t promise I’ll implement them 🙂

Linus (torvalds@kruuna.helsinki.fi)

PS. Yes – it’s free of any minix code, and it has a multi-threaded fs.
It is NOT protable (uses 386 task switching etc), and it probably never
will support anything other than AT-harddisks, as that’s all I have :-(.