1. ติดตั้ง packages : tdsodbc libct3 unixodbc php5-odbc #apt-get install tdsodbc libct3 unixodbc php5-odbc
2. สร้างไฟล์สำหรับกำหนดตัว Driver #vim /etc/freetds/tds.driver.template และมีเนื้อหาไฟล์ดังนี้
File : /etc/freetds/tds.driver.template
[TDS]
Description = FreeTDS Driver for Linux & MSSQL on Win32
Driver = /usr/lib/odbc/libtdsodbc.so
Setup = /usr/lib/odbc/libtdsS.so
3. สร้างไฟล์สำหรับกำหนดตัว Domain #vim /etc/freetds/tds.dsn.template และมีเนื้อหาไฟล์ดังนี้
File : /etc/freetds/tds.dsn.template
[DSN_NAME]
Description = Test to freeTDS
Driver = TDS
Trace = No
Database = DefaultDatabase [replace with your database name]
Server = mysqlserver.inter.net [replace with your SQL server's host]
Port = 5050 [replace with the port that SQL is listening on]
4. ทำการนำไฟล์ทั้งสองเข้าสู่ระบบ odbc #odbcinst -i -d -f /etc/freetds/tds.driver.template #odbcinst -i -s -f /etc/freetds/tds.dsn.template
5. ทำการนำไฟล์ odbc.ini ที่สร้างในโฟลเดอร์ root ของเรามาไว้ใน /etc และอย่าลืม backup ไฟล์ด้วยครับเผื่อมีปัญหา #mv /etc/odbc.ini /etc/odbc.ini.bak #cp /root/.odbc.ini /etc/odbc.ini
6. ทำการ restart โปรแกรมเวบเซิร์ฟเวอร์ใหม่ #/etc/init.d/apache2 restart
7. ทดสอบการติดต่อดูครับด้วยคำสั่ง #isql -v DSN_NAME hatto passw โดย DSN_NAME คือชื่อ Domain ส่วน hatto คือชื่อ user และ passw คือรหัสผ่าน ถ้าสามารถติดต่อได้ก็จะขึ้น prompt ให้ใส่คำสั่ง SQL ครับ
8. ส่วนการติดต่อในโปรแกรม php สามารถใช้คำสั่ง odbc_connect ได้เลย
Sample ##
cr : http://www.unixodbc.org/doc/FreeTDS.html
<?php
# connect to a DSN "DNS_NAME" with a user "cheech" and password "chong"
$connect = odbc_connect("MSSQLTest", "cheech", "chong");
# query the users table for all fields
$query = "SELECT * FROM users";
# perform the query
$result = odbc_exec($connect, $query);
# fetch the data from the database
while(odbc_fetch_row($result)) {
$field1 = odbc_result($result, 1);
$field2 = odbc_result($result, 2);
print("$field1 $field2\n");
}
# close the connection
odbc_close($connect);
?>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น