realistTheorist Posted June 20, 2008 Share Posted June 20, 2008 [Mods, if this is the wrong sub-forum; please move. Thx - grateful noob] After recently working through accessing MS Sql-Server using PHP's "PDO" extension, I made a list of instructions. Thought I would share them here: Environment: Windows XP-Pro, Apache 2.0.x, PHP 5.2.6 Pre-requisites: 1) MS SQL Server is installed and working. Also, can access MS SQL 2005 via ODBC (all set up in Windows ODBC Admin is complete and tested) 2) Apache and PHP are working together (a simple PHP page displays) [if you are just setting up PHP, and using the Windows installer, onn the screen to choose "extensions", find the "PDO" option and select two sub-options: "ODBC/DB2", "MS Sql" from there.] 3) Programmer is familiar with combined PHP and SQL usage (e.g. using MySql), but wants to move to specifically PDO Then, you must do the following: 4) If you already have PHP in place, look in the "ext" sub-directory under your main PHP directory, for three DLL files: php_pdo.dll,php_pdo_odbc.dll, php_pdo_mssql.dll If these files are missing, download them from the PHP site (extract from the zip file) (4.1) Open you PHP.ini file and search for the following. If they are not in the file, add them, near the end: [php_PDO] extension=php_pdo.dll [php_PDO_ODBC] extension=php_pdo_odbc.dll [php_PDO_MSSQL] extension=php_pdo_mssql.dll This instructs PHP to load the four DLLs that you just downloaded to the ext directory. (4.2) Also, search your php.ini file to ensure that the setting "extension_dir" points to your ext directory. An example would be: extension_dir ="C:/PHP/ext" (4.3) If you have a second php.ini file (example in your Windows directory), remember to replace that one. 5) (5.1) Two dlls from your main PHP directory must be made accessible. They can be copied to the Apache/bin directory: libmysql.dll and ntwdblib.dll. (5.2) Ensure that your main PHP directory (e.g: c:\php) is a part of your "PATH" variable. If not, add it to your path. 6) Restart your web-server and check the logs (with default Apache installation, check the file "error.log" in the "logs" sub-directory) to see if there are any error messages about the dlls above not being loaded. If there are such messages (e.g. "Could not load php_pdo_mssql.dll"), recheck that all the steps above were all carried out. 7) If there are no such errors, you are ready to test the PHP-PDO extension. You can use the script below (Save it as a file, named testpdo.php). (7.1) Familiarize yourself with what this test-script does. It attempts to create a table, named "testtblx" in your database. This table has only two fields "fld1" and "fld2". It attempts to insert records into the table, and then it uses a SQL select to display the records. It then attempts to do an update and displays the records again. This brief test demonstrates the Select, Insert and Update features. (7.2) The PHP test script is provided on an "AS IS" basis, so a PHP programmer should read through it to ensure that it is ready for test. If you already have a table named "testtblx" in your database, edit the script to use a different table-name. Better still, use a complete new test-database for this. (7.3) In your Windows ODBC Admin, go to the "System DSN" panel and find the DSN name for the SQL Server data-source on which you wish to run the test. In the attached file (testpdo.php), find the line that says "$dsn='odbc:sqlsvr2005';" Replace the name "sqlsvr2005' to be the DSN that you have set up in ODBC. For instance, if you has set it up as "mysqltest", then change the line to read: $dsn='odbc:mysqltest'; (7.4.) After editing to meet your systems naming, and pointing to your test database, place the file on your web-server. (For instance on a typical Apache installation, you can simply copy it to the "htdocs" sub-directory under Apache. (7.5) Using your web-browser, type in the URL that would access the file. For instance, if you are testing on a local machine, rather than remotely, you would enter the URL: http://localhost/testpdo.php If remote, you would replace "localhost" with whatever IP or domain-name is appropriate (7.6) When the script runs, you should see a "connected" message followed by various other SQL prints-outs and Error-prints. If you do not see the "connected" message, check your web-server logs. You might see a message there ("e.g. Datasource DSN name mytestSql not found"). Otherwise, you will see the output that was expected. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.