Wednesday, March 11, 2009

FreeBSD PHP MS SQL FreeTDS Howto

When you install php5-extensions, make sure you select the "sybase_ct" option.

After insllation,

# cd /usr/local/etc
# cp freetds.conf.dist freetds.conf

# vi freetds.conf
[global]
# TDS protocol version
tds version = 8.0
[MyMSSQL]
host = 192.168.200.119
port = 1433
tds version = 8.0
client charset = UTF-8

test.php

$adodb_dbdriver = "mssql";
$adodb_server = "MyMSSQL";
$adodb_user = "danny";
$adodb_password = "123456";
$adodb_database = "my_member";

$conn = ADONewConnection($adodb_dbdriver); # eg 'mysql' or 'postgres'

$conn->Connect($adodb_server, $adodb_user, $adodb_password, $adodb_database);

$conn->debug = true;
$conn->SetFetchMode(ADODB_FETCH_ASSOC);

$strSQL = "SELECT TOP 10 ";
$strSQL .= "id, title, dateadded ";
$strSQL .= "FROM news ";
$strSQL .= "ORDER BY id DESC ";

$rs = $conn->Execute($strSQL);

$rowCount = 0;

if ($rs) {
while($row = $rs->FetchRow()) {
$title = $row['title'];
//$title = iconv("", "UTF-8", $title); // only used when the field is varchar. No need for nvarchar field
}
}

No comments: