ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[±Ð¾Ç] (PHP+IIS+MsSQL±Ð¾Ç²Ä23½g) ¸ê®Æ®wÅX°Êµ{¦¡»P ODBC ³sµ²

[±Ð¾Ç] (PHP+IIS+MsSQL±Ð¾Ç²Ä23½g) ¸ê®Æ®wÅX°Êµ{¦¡»P ODBC ³sµ²

²{¤µªººô¸ô¹q¤l°Ó°È§K¤£¤F­n»P¸ê®Æ®w§è¤WÃö«Y¡A³o¸Ì©Ò«üªº¸ê®Æ®w¬O¡GWeb Server ¤Wªº¸ê®Æ®w³á¡I¨ä¹ê¸ê®Æ®w¤£´N¬O¤@­ÓÀɮסA¦ý¬O¡A¥¦¬O¤@­Óµ²ºc¤ÆÀx¦s¸ê®Æªº®æ¦¡ÀɮסC¨ä¹ê§Ú­Ì±`±`¦b¨Ï¥Î¸ê®Æ®w¦Ó¤£¦Ûª¾¡A«ç»ò»¡¡H§A¦³¨S¦³¾Þ§@¹L.mdb ªºÀÉ®×°Ú¡H¥¦´N¬O¤@­Ó Access ®æ¦¡ªº¸ê®Æ®wÀɮסC


¸ê®Æ®wÅX°Êµ{¦¡
§Ú­Ì¦s¨ú¥ô¦ó¤@ºØ¸ê®Æ®w³£¥²¶·¦³¨ä¬Û¹ïÀ³ªº¸ê®Æ®wÅX°Êµ{¦¡¡A¤~¯à°÷¹ï¸ê®Æ®wªºÀɮק@¦s¨úªº°Ê§@¡I

  • ODBC (Open DataBase Connectivity)
  • OLE DB (Object Linking and Embedding DataBase)
¥H¤W³o¨âªÌ¬Ò¬°ÅX°Êµ{¦¡Ãþ§O¡AÀ³¥Îµ{¦¡¥iÂǥѦ¹ÅX°Êµ{¦¡¤¶­±¨Ó¦s¨ú¸ê®Æ®wªº¸ê®Æ¡A¦p¤U¹Ï©Ò¥Ü¡C
CH7_01.png


¬d¬Ý¤w¦w¸ËªºÅX°Êµ{¦¡
¦b§Ú­Ìªº¾÷¾¹¤W¨ì©³¦w¸Ë¤F­þ¨ÇODBC¸ê®Æ®wÅX°Êµ{¦¡©O¡H­n¬d¬Ý§Ú­Ì¾÷¾¹¤W¦w¸Ë¤F­þ¤@¨Ç¸ê®Æ®wÅX°Êµ{¦¡¡A­º¥ý¡A§Ú­Ì¥²¶·¥ý¥´¶}¡y±±¨î¥x¡z

CH7_02.png (78.89 KB)

CH7_02.png

CH7_03.png (22.68 KB)

CH7_03.png

¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

ODBC³sµ²¸ê®Æ®w
ODBC³sµ²¸ê®Æ®wªº¤èªk¤À¬°¨âºØ¡A¤@ºØ¬°¡uDSN¡]Data Source Name¡^¡v¡A¥t¤@ºØ¬°¡uDSN-less¡v¡G¡uDSN¡vªº¤è¦¡¥²¶·³z¹L¡uODBC¸ê®Æ¨Ó·½ºÞ²z­û¡vªº³]©w¡A³o¤è¦¡¤ñ¸û¤£«Øij±Ä¥Î¡A¦]¬°³]©w¹Lµ{Ácº¾¡A­Y«D¬O¦Û¤vªº¾÷¾¹®£©È¤£®e©öÀò±o³]©wªºÅv­­¡A¦Ó¡uDNS-less¡v¤è¦¡«h¬O¤@­ÓÅܳqªº¤è¦¡¡A¦b³sµ²¸ê®Æ®wªº¹Lµ{µ¹¤©¾A·íªº³sµ²°Ñ¼Æ§Y¥i¡C



³sµ²¸ê®Æ®wªº¨ç¼Æ
­n¹ï¸ê®Æ®w¦s¨ú¸ê®Æ¥²¶·¥ý¶i¦æ¸ê®Æ®wªº³sµ²ªº°Ê§@¡A´N¦p¾m§Ú­Ì­nŪ¨úÀɮ׸ê®Æ®É¥Îfopen()¨ç¼Æ¶}±ÒÀɮפ@¯ë¡A­n¹ïAccess¡BSQL Server¶i¦æ³sµ²°Ê§@¥i¨Ï¥ÎODBC¸ê®Æ®w¨ç¼Æ¡G¡uodbc_connect()¡v©Î¡uodbc_pconnection()¡v¨ç¼Æ¡C
odbc_connect() ¨ç¼Æ
  1. resource odbc_connect ( string dsn, string user, string password [, int cursor_type])
½Æ»s¥N½X
·í¸ê®Æ®w³sµ²¦¨¥\«h¦^¶Ç³s½uªº½s¸¹¡A­Y³s½u¥¢±Ñ«h¦^¶Ç0¡C


odbc_pconnect() ¨ç¼Æ
  1. resource odbc_pconnect ( string dsn, string user, string password [, int cursor_type])
½Æ»s¥N½X
·í¸ê®Æ®w³sµ²¦¨¥\«h¦^¶Ç³s½uªº½s¸¹¡A­Y³s½u¥¢±Ñ«h¦^¶Ç0¡C


odbc_close() ¨ç¼Æ
  1. void odbc_close ( resource connection_id)
½Æ»s¥N½X
odbc_connect()¨ç¼Æ¬O¼È®É©Êªº³s½u¡A¨C¦¸³sµ²³£·|¨Ï¸ê®Æ®w²£¥Í¤@­Ó·sªº³s½uªÌ¡A©Ò¥H¨C¦¸³sµ²«á¡A·í¤£»Ý­n¦¹³s½u®É´N¸Ó¥Î¡uodbc_close()¡v¨ç¼ÆÃö³¬³sµ²¡A¤£µM±zªº¸ê®Æ®w©Î¦øªA¾¹­t¾á¤Ó­«´N·|¥X°ÝÃD¡C

odbc_pconnect()¨ç¼Æ¬O¿W¥e©Êªº³s½u¡A·í»P¸ê®Æ®w©¹ªðªº¸ê®Æ¤j®É¡A¥i¨Ï¥Î¦¹¨ç¼Æ³sµ²¸ê®Æ®w¡A§Y¨ÏPHPµ{¦¡¤w¸g°õ¦æ§¹²¦¡A¦¹³s½u¨ÌµM¦s¦b¡A­Y¦¹³s½u¼È®É¨S¦³¥Î¨ì«h³s½u¶i¤J¡u¥ð¯v¡]sleep¡^¡vª¬ºA¡C

¹ï©óSQL Server¡A°£¤F¥i¨Ï¥ÎODBC¸ê®Æ®w¨ç¼Æ¸s¥~¡APHP¥ç´£¨Ñ¤FSQL Server±MÄݪºMSSQL¨ç¼Æ¸s¡A¦]¦¹³sµ²SQL Server¸ê®Æ®w¤]¥i¥H¨Ï¥Î¡umssql_connect()¡v©Î¡umssql_pconnection()¡v¨ç¼Æ¡A¦pªG±zªº¸ê®Æ«áºÝ¬°SQL Server¡A«Øij±z¨Ï¥ÎSQL Server±MÄݪºMSSQL¨ç¼Æ¸s¡C

mssql_connect() ¨ç¼Æ
  1. int mssql_connect ( [string servername [, string username [, string password]]])
½Æ»s¥N½X
·í¸ê®Æ®w³sµ²¦¨¥\«h¦^¶Ç³s½uªº½s¸¹¡A­Y³s½u¥¢±Ñ«h¦^¶Ç0¡C


mssql_pconnect() ¨ç¼Æ
  1. int mssql_pconnect ( [string servername [, string username [, string password]]])
½Æ»s¥N½X
·í¸ê®Æ®w³sµ²¦¨¥\«h¦^¶Ç³s½uªº½s¸¹¡A­Y³s½u¥¢±Ñ©Îµo¥Í¿ù»~«h¦^¶Çfalse¡C



DNS-less³sµ²¸ê®Æ®w
³sµ²Access2000¸ê®Æ®w
³sµ²Access2000¸ê®Æ®w¬O¨Ï¥Îodbc_connect()¨ç¼Æ¡G
  1. odbc_connect(¡§ODBC¦r¦ê¡¨,¡¨±b¸¹,¡¨±K½X¡¨)
½Æ»s¥N½X
°ò¥»¤W¡A³sµ²Access2000¸ê®Æ®w®É¡AODBC¦r¦ê¤¤»Ýµ¹©w¨â­ÓODBC°Ñ¼Æ­È¡G

  • Driver¡G³sµ²Access2000¸ê®Æ®w©Ò»ÝªºODBCÅX°Êµ{¦¡¡C
  • DBQ¡GAccess2000¸ê®Æ®wªº¥»¾÷¡u¹ê»Ú¸ô®|¡v¡C

¥H³sµ²Access2000®æ¦¡ªºstudents1.mdb¸ê®Æ®wÀɮ׬°¹ï¶H¡G
  1. <?
  2. $Provider="Driver={Microsoft Access Driver (*.mdb)};";
  3. $Path="DBQ=" . getcwd() . "\\" . "STUDENTS1.mdb";
  4. //³sµ²¸ê®Æ®w
  5. odbc_connect($Provider . $Path,"","");
  6. ?>
½Æ»s¥N½X

  • ¡u$Provider="Driver={Microsoft Access Driver (*.mdb)};"¡v¡G¨Ï¥ÎÅܼÆProvider¨Ó«ü©w§Ú­Ì­n¨Ï¥Îªº¸ê®Æ®wÅX°Êµ{¦¡¡A¦]¬°§Ú­Ì­n¶i¦æ³sµ²¦s¨úªº¸ê®Æ®wÀÉ®×Ãþ«¬¬°Access®æ¦¡¡A©Ò¥H¨Ï¥ÎªºODBC¸ê®Æ®wÅX°Êµ{¦¡¬°¡G¡u{Microsoft Access Driver (*.mdb)}¡v¡C
  • ¡u$Path="DBQ=" . getcwd() . "\\" . "STUDENTS1.mdb"¡v¡G¨Ï¥ÎÅܼÆPath¨Ó«ü©w§Ú­Ì­n¨Ï¥Îªº¸ê®Æ®w¸ô®|»PÀɦW¡F§Ú­Ì­n¶i¦æ³sµ²¦s¨úªº¸ê®Æ®wÀɮסA¥²¶·«ü©w¦¹¸ê®Æ®wÀɮתº§¹¾ã¸ô®|¦WºÙ¡A©Ò¥H§Ú­Ì¥²¶·¨Ï¥Î¡ygetcwd()¡z¤èªk¨Ó¨ú±o¥Ø«e«°¥«°õ¦æªº§¹¾ã¸ô®|¦WºÙ¡A±µµÛ¦A¥[¤W¸ê®Æ®wªº¬Û¹ï¸ô®|»P¸ê®Æ®wÀɮצWºÙ¡C




³sµ²SQL2000¸ê®Æ®w
SQL¸ê®Æ®wªº³sµ²¥²¶·¹ïÀ³ºÞ²z¸ê®Æ®wªºSQL Server¡A©Ò¥H³sµ²ªº°Ñ¼Æ¤ñ¸û¦h¡G

  • Driver¡G³sµ²SQL Server©Ò»ÝªºODBCÅX°Êµ{¦¡¡C
  • Server¡G±ýµn¤JªºSQL Server¥D¾÷¦WºÙ¡C
  • UID¡Gµn¤JSQL Serverªº±b¸¹¡C
  • PWD¡Gµn¤JSQL Serverªº±K½X¡C
  • DataBase¡Gµn¤JSQL Server«á­n³sµ²¨Ï¥ÎªºSQL¸ê®Æ®w¦WºÙ¡C

¥H³sµ²SQL2000¤º«ØªºNorthwind¡]¥_­·¡^¸ê®Æ®wÀɮ׬°¹ï¶H¡G
  1. <?
  2. $dnsless="Driver={SQL Server};" .
  3.          "Server=(Local);" .
  4.          "UID=SA;" .
  5.          "PWD=12345;" .
  6.          "DataBase=Northwind";
  7. //³sµ²¸ê®Æ®w
  8. $z=odbc_connect($dnsless,"","");
  9. echo $z;
  10. ?>
½Æ»s¥N½X
´£¥Ü¡I¡I
Server°Ñ¼Æ­È¡u¡]Local¡^¡v¥Nªíµn¤JªºSQL Server¬°¥»¾÷¦øªA¾¹¡A­Y«D¥»¾÷¦øªA¾¹¡A«hServer°Ñ¼Æ­È¥²¶·«ü©w¬°±ýµn¤JªºSQL Server¥D¾÷¦WºÙ¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

DSN³sµ²¸ê®Æ®w
¡uDSN-less¡v¤è¦¡¬O¦b³sµ²¸ê®Æ®w®É¡A§¹¾ãªº«ü©ú¸ê®Æ®wÅX°Êµ{¦¡¡B¸ê®Æ®wÀɮצì¸m¡Kµ¥µ¥¡F¦Ó¡uDSN¡v¤è¦¡«h¬O±N³sµ²¸ê®Æ®w®É©Ò»Ýªº°Ñ¼Æ¸ê®Æ¹w¥ý³]©w¦n¡A±N³o¨Ç¸ê°T¥æ¥Ñ¡uODBC¸ê®Æ¨Ó·½ºÞ²z­û¡v¶°¤¤Àx¦s»PºÞ²z¡C
¨Ï¥Î¡uODBC¸ê®Æ¨Ó·½ºÞ²z­û¡v³]©wDSN¸ê®Æ¨Ó·½ªº¨BÆJ¦p¤U¡A¥H³]©wSQL Server¸ê®Æ¨Ó·½¬°¨Ò¡G

  • ¥´¶}¡y±±¨î¥x¡z=>¡y¨t²ÎºÞ²z¤u¨ã¡z¡AµM«á¦A¶}±Ò¡yODBC ¸ê®Æ¨Ó·½¡z¶µ¥Ø¡C
  • ¦b¡uODBC¸ê®Æ¨Ó·½ºÞ²z­û¡v¤Á´«¾Þ§@­¶ÅÒ¬°¡y¨t²Î¸ê®Æ¨Ó·½¦WºÙ¡z¡C


  • ¦b¡y¨t²Î¸ê®Æ¨Ó·½¦WºÙ¡z­¶ÅÒ¤¤«ö¤U¡u·s¼W¡v«ö¶s¡A¦¹®É¡A±N·|¥X²{¡u«Ø¥ß·s¸ê®Æ¨Ó·½¡vµøµ¡¡A©óµøµ¡¤¤¿ï¨ú¡uSQL Server¡v¡A±µµÛ«ö¤U¡u§¹¦¨¡v«ö¶s¡G



  • ©ó¡u«Ø¥ß·s¸ê®Æ¨Ó·½¡vµøµ¡¤¤«ö¤U¡u§¹¦¨¡v«ö¶s«á¡A±µµÛ¥X²{¡u«Ø¥ß·sªº¸ê®Æ¨Ó·½¦ÜSQL Server¡vµøµ¡¡A¦b¦¹µøµ¡¤¤¡A§Ú­Ì¥²¶·¬°·sªº¸ê®Æ¨Ó·½©R¦W¡A¨Ã«ü©w­n³s½uªºSQL Server¡A­Y¬O¥»¾÷¦øªA¾¹¡A«h¿ï¨ú¡ulocal¡v§Y¥i¡A¿é¤J§¹²¦«á«ö¤U¡u¤U¤@¨B¡v«ö¶s¡C



  • ±µ¤U¨Ó¬°³]©wÅçÃÒ¼Ò¦¡»Pµn¤J±b¸¹¡B±K½X¡Aµn¤JÅçÃÒªº¼Ò¦¡½Ð¿ï¾Ü¡u²V¦X¼Ò¦¡¡]¥Ñ¨Ï¥ÎªÌ©Ò¿é¤Jªºµn¤JÃѧO½X¤Î±K½X¶i¦æSQL Server±b¤áÅçÃÒ¡^¡v¡A¦Ü©ó±b¸¹±K½X¥ý¥Î§Ú­Ì¦w¸ËSQL Server®ÉªººÞ²z­û¡]sa¡^±b¸¹¸ò±K½X¡A¦Ü©óSQL Server¨Ï¥ÎªÌ±b¸¹ªº³]©w¡A¦pªG«áÄò¦³¥Î¨ì§Ú­Ì¦A½Í¡C


  • «ü©w¹w³]¸ê®Æ®w¡A³]©w¦¨¥\µn¤JSQL Sserver©Ò­n¨Ï¥Îªº¸ê®Æ®w¡C



  • ¦A¨Ó¬°¬ÛÃöªº»Ý¨D³]©w¡A±Ä¥Î±ý³]­È§Y¥i¡Aª½±µ«ö¤U¡u§¹¦¨¡v«ö¶s¡C



  • ±µ¤U¨Ó¥X²{ªº§Ú­Ì­è­è³]©wªº°T®§¤º®e³ø§i¡A¥ý§O«æµÛ«ö¡u½T©w¡v«ö¶s¡A­n¥ý´ú¸Õ¤@¤U³]©w¬O§_¥¿½TµL»~¡C




  • «ö¤U¡u´ú¸Õ¸ê®Æ¨Ó·½¡v«ö¶s¡A¶i¦æ¸ê®Æ®w³sµ²´ú¸Õ¡A¦pªG¥X²{¤U¹Ïªºµ²ªG¡A´Nªí¥Ü§Ú­Ì³]©w¦¨¥\¤F¡G



  • «ö¤U¡u½T©w¡v«ö¶s¡A¦^¨ì¹Ï7-10ªºµøµ¡µe­±¤¤¡A¦A¦¸«ö¤U¡u½T©w¡v«ö¶s¦^¨ì¡uODBC¸ê®Æ¨Ó·½ºÞ²z­û¡vµøµ¡¤¤¤Î¥iµo²{Á~¸êªº¨t²Î¸ê®Æ¨Ó·½¡G



·í§Ú­Ì§¹¦¨¤FDSNªº³]©w«á¡A¦b¨Ï¥Îodbc_connect()¨ç¼Æ³sµ²¸ê®Æ®w®É¡AODBC¦r¦ê°Ñ¼Æ´N¥u­nµ¹¨t²Î¸ê®Æ¨Ó·½¦WºÙ¡umySQL¡v¡B±b¸¹»P±K½X§Y¥i¡C
odbc_connect("DSN¦WºÙ","±b¸¹","±K½X");
¨Ò¦p¡G
  1. <?
  2. $y=odbc_connect("mySQL","sa","12345");
  3. echo $y;
  4. ?>
½Æ»s¥N½X
¨Ï¥Î
mssql_connect()¨ç¼Æ³sµ²¸ê®Æ®w®É¡AODBC¦r¦ê°Ñ¼Æ¤@¼Ëµ¹¨t²Î¸ê®Æ¨Ó·½¦WºÙ¡umySQL¡v¡B±b¸¹»P±K½X§Y¥i¡C
  1. <?
  2. $y=mssql_connect("mySQL","sa","12345");
  3. echo $y;
  4. ?>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¤£¦n·N«ä
§Ú¦b DSN³sµ²¸ê®Æ®w ¦³¿ù»~°T®§
¿ù»~°T®§¬O¡G³s½u¥¢±Ñ»P¤£¦s¦b©Î©Úµ´¦s¨ú
§Ú·ÓµÛ±zªº¨BÆJ°µ
¦ý¬O¦³¿ù¤F
½Ð°Ý¤p»x¤j¤j
­n¦p¦ó§ïµ½©O¡H
§ÚOS¬OWin7
Hello World

TOP

¤£¦n·N«ä
§Ú¦b DSN³sµ²¸ê®Æ®w ¦³¿ù»~°T®§
¿ù»~°T®§¬O¡G³s½u¥¢±Ñ»P¤£¦s¦b©Î©Úµ´¦s¨ú
§Ú·ÓµÛ±zªº¨BÆJ°µ
¦ý ...
a01456655 µoªí©ó 2013-8-7 13:39


§A³sµ²¸ê®Æ®wªº±b¸¹±K½X¬O¥¿½Tªº¶Ü??
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

§Ú¦bºô¸ô¤W§ä¨ì¸Ñ¨M¤èªk¤F¡Aµ¹ª©¤j¬Ý¬Ý¬O§_¦³®t§O
¦p¤U¡G
$conn = mysql_connect("127.0.0.1",'userid','password') or die("can't open this DB");
ª©¤j±z¥Îªº¬O  mssql ¦ý§Ú¥Î§O¤H±Ðªº  mysql ´N¥i¥H³s¤W
mssql »P mysql ³o¤G­Ó¨ç¦¡¦³¤°»ò®t§O¶Ü??
Hello World

TOP

§Ú¦b²Ä4¨BÆJ´N¨S¦³¥X²{ (local)
§Ú¬O¤â°Ê¦Û¤v¥´¤W (local)
¥i¥H°õ¦æ¤U¤@¨B
¦ý¬O¦b²Ä5¨BÆJ  ¿é¤J±b±K³o¥´§¹«á«ö¤U¤@¨B´N¥X²{¤F¿ù»~°T®§
¦³¨S¦³¤èªk¥i¥H¬dª¾¦Û¤v¬O§_¦³¸ê®Æ®w©O ??
Hello World

TOP

§Ú¦b²Ä4¨BÆJ´N¨S¦³¥X²{ (local)
§Ú¬O¤â°Ê¦Û¤v¥´¤W (local)
¥i¥H°õ¦æ¤U¤@¨B
¦ý¬O¦b²Ä5¨BÆJ  ¿é¤J±b±K³o¥´ ...
a01456655 µoªí©ó 2013-8-8 09:26



    ¥»½g±Ð¾Ç°w¹ï ·L³nªºSQL SERVER
½Ð½T»{§A¤w¦w¸Ë SQL SERVER
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

        ÀR«ä¦Û¦b : ¤H¥Í³Ì¤jªº¦¨´N¬O±q¥¢±Ñ¤¤¯¸°_¨Ó¡C
ªð¦^¦Cªí ¤W¤@¥DÃD