Installing PHP driver for MongoDB in WAMP

I’ve been getting more curious about MongoDB. The problem is I’ve been struggling trying to create a development environment. I’ve tried OSX, Ubuntu, and Windows. I always get stuck at a step. I think I found the right recipe.

First, I’m running Windows 7 Professional 64 bit virtually. You don’t have to run Windows virtuall. I’m mostly on my Mac and I prefer to use virtual environments for development so it’s okay if I screw around with things. Then I installed Wamp. The key here is to use the 32bit version of Wamp. I’ve tried the 64bit version of Wamp and could not get it to work. Then, you need to download the latest driver from the MongoDB website. You will get a NTS (non-thread safe) and TS (thread safe) driver. The folders are labeled as follows “mongo-[version]-php5.3vc9” and “mongo-[version]-php5.3vc9ts. Remember “ts” = “thread safe”. I used the non-thread safe DLL file. Take that DLL file and copy and paste it in the “ext” folder within the PHP installation directory of Wamp. If you installed using default settings it should be “c:\wamp\bin\php\php5.3.x\ext”. Open the php.ini file and add the php_mongo.dll extension. Restart Apache and it should show up in your phpinfo() page.

I will be using MongoHQ to try things out. I’m not sure why I couldn’t get it to work in a 64bit web server environment. Mongo looks very cool and if it can help me decrease development time, I’m going to build future web apps with it.

No more Hostasp.net

After a few days of playing around with their server and having some difficulties, I decided to cancel their service. I’m new to Windows hosting since I’ve always used Linux hosting, so I’m not that all familiar with it. A lot of my questions can easily be resolved if they had a better FAQ or knowledge base section on their website.

If they had a local US number I can call, it would make getting support a bit easier and faster. I used to complain that Godaddy has phone support but it was long distance, but it’s still a US number. Besides, long distance is a thing of the past within the US since it’s pretty much a local call with most cell phone plans. I just thought it was weird that they didn’t use an 800 number.

Instead, I had to create a ticket in their system, which isn’t bad but the response time averaged over 6 hours. They also had a chat software installed on their website but being located outside the US, the hours were different and most of the time it’s offline. At that rate, I figured a few weeks before I can get settled in and it’s unreasonable. Only solution I had was to cancel the service and find one closer to home or at least easily accessible within the US.

Don’t get me wrong, the features you get for the price they are charging are great. You get an IP address, unlimited databases (MSSQL 2008 and MySQL), remote connection to the database, web deploy compatibility, and refund any time. If my comfort level and knowledge with Windows hosting environment matched with Linux, I wouldn’t need that much support. I think this company is new and has a potential of becoming better. Once I get more familiar with Windows servers, I may look at them again.

MSSQL Driver for PHP5.3

Before using PHP5.3, I never had to worry about MSSQL support. PHP included a dll file to use with MSSQL Server 2000 and up. This is no longer true if you decide to upgrade to PHP5.3.x. For months I looked for ways to connect to our MSSQL Server using PHP5.3.x. I finally found the driver and it works. It’s called SQLSRV and you can download it from http://sqlsrvphp.codeplex.com. It’s easy to install, just read the CHM file.

  • Run the exe to unpack it
  • Rename the folder to Microsoft SQL Server Driver for PHP (optional)
  • Move this to your %Program Files% directory (optional)
  • Inside that directory you will find several dll files. It supports both NTS (Non Thread Safe) and TS (Thread Safe). You also need to choose between VC6 or VC9 compiler. This will determine which file will be used.
  • Copy that file to %Installation Directory%\PHP\ext\ (assuming you are using the ext directory for your extensions.
  • Edit your php.ini. In the extensions section add the following extension=php_sqlsrv_53_ts_vc6.dll – or whatever file you copied to your ext directory.
  • Restart your web server and that should be it.

Here’s a quick sample code to get you started.

<?php

$servername = "SQLSERVER_NAME_OR_IP";
$uid = "db_username";
$pwd = "db_passwd";
$connectionInfo = array (
"UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"database_name"
);
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn === false) {
echo "ERROR: DB Connection";
die(print_r(sqlsrv_errors(), true));

}
$stmt = sqlsrv_query($conn, "SELECT * FROM table");

if ($stmt) {
while ($row = sqlsrv_fetch_array($stmt)) {
echo $row[0].’<br />’;
}
}

?>

That should do it. I have only tested this on Windows 7 Professional (32bit), MSSQL 2005 (32bit) Standard on a remote Windows 2008 Server Standard R2, PHP5.3.3, Apache2.2.15, and driver version 1.1 (version 2 is now available) . Make sure your MSSQL server can accept remote connections if it is on a different server than your web server. The above instructions shouldn’t be  too different on other versions.

If you have a phpinfo() page, you will see the image below.

sqlsrv_phpini

Hope this helps you get started. Happy coding.

XHTML compliant with ampersands in the URL

I was having a few problems creating a page with ampersands in the URL. This is a quick fix if it’s part of the HTML by using &amp; but if it’s part of a javascript, it will take the code literally. For example, let’s say you have a javascript function that sends you to a URL when you click a button.

<script type=”text/javascript”>

function clickMe() {
window.location = ‘http://www.google.com/search?q=javascript+ampersand+xhtml&client=firefox-a’;
}

</script>

If you are concerned about being XHTML compliant, you will get an “unknown entity section” error. You can get some more info of the error here http://htmlhelp.com/tools/validator/problems.html. You cannot use “&” in the URL of that javascript function. You can use &amp; (HTML) or &#38; (ASCII). It will solve your error but depending on the browser/version, it will send the code to the URL and will result in an error – especially if your code is dependent on the querystring. To fix this you can use CDATA.

/*<![CDATA[*/

window.location = 'http://www.google.com/search?q=javascript+ampersand+xhtml&client=firefox-a';

/*]]*/

The /**/ ignores the lines of code but because your document is XML formatted, CDATA parse the line in between properly (http://www.w3schools.com/xml/xml_cdata.asp). I got answer from http://www.liferay.com/community/forums/-/message_boards/message/2657431#_19_message_2873280.

Windows 2008 and MSSQL Server 2005 not allowing remote connections

If you think you checked every setting in MSSQL Server that allows remote connections, you may have missed the Windows firewall setting. I have installed MSSQL 2005 many times on Windows 2000 Server, 2003, and 2008 and never ran into the remote connection problem. What’s happening is I’m unable to connection using the Management Studio on another machine into the server.

  • Checked the Configuration Tools/SQL Server Configuration Manager
  • Checked SQL Browser service is running
  • Pinged the IP address
  • Checked the Surface Area Configurations and made sure it allowed local and remote connections using TCP/IP only

It turns out that Windows firewall didn’t add MSSQL to the exceptions list. So here’s how you do it. Run firewall.cpl to bring up the Windows Firewall settings. On the left click on “Allow a program through Windows Firewall”. Make sure you are on the “Exceptions” tab and click on “Add program…”. If it’s not on the list, browse for the sqlservr.exe. You will find this in [INSTALL_DIR]\MSSQL.1\MSSQL\Binn\sqlservr.exe.

I never had this issue before. Not sure what caused it. Maybe Windows 2008 SP2. The versions I’m using are Windows Server 2008 RC2 and MSSQL Server 2005 Standard.