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

[±Ð¾Ç] ²Ä8³¹ PHP»P¸ê®Æ®w¦s¨ú(8-1 ¬d¸ß»P¨ú±o¸ê®Æ)

[±Ð¾Ç] ²Ä8³¹ PHP»P¸ê®Æ®w¦s¨ú(8-1 ¬d¸ß»P¨ú±o¸ê®Æ)

¸g¹L¤W¤@³¹ªº¬~§¡A¬Û«H±z¹ï©ó¸ê®Æ®w»P¸ê®Æªí¤w¦³°ò¦ªº»{ÃÑ¡A¦P®É¡A±zÀ³¸Ó¤]¤w²M·¡ªºª¾¹D¡G­n¦bPHP¤¤³sµ²Access¸ê®Æ®w©ÎSQL Server¥i¥H³z¹Lodbc_connect()¨ç¼Æ¡A¦Ó³sµ²MySQL Server«h¥i¥H¨Ï¥Î±MÄݪºmysql_connection()¨ç¼Æ¡C

¦]¬°¥»®Ñªº¥D¨¤¤§¤@¬°MySQL Server¡A©Ò¥H«áÄò³¹¸`±N¥H¨Ï¥ÎMySQL Server±MÄݪºmysql¨ç¼Æ¬°¾Ç²ß­«ÂI¡C

mysql_connection()¨ç¼Æ¥u­t³d³sµ²¸ê®Æ®w¡A¦Ü©ó¦p¦ó¦s¨ú¸ê®Æªí¤ºªº¸ê®Æ¥i´N¬£¤£¤W¥Î³õ¤F¡A­n¦s¨ú¸ê®Æ¥²¶·¾amysql_query()¡Bmysql_fetch_row()¡Bmysql_fetch_field()µ¥µ¥¨ç¼Æ¡C

¥»³¹¤º®e¬Ò¥HMySQL¸ê®Æ®w¬°¨Ï¥Î¹ï¶H¡A¦b½d¨Ò¥úºÐ\DataBase¥Ø¿ý¤¤¦³¤@­ÓMySQL¸ê®Æ®w¡uStudent¡v¡AÀɮצWºÙ¬°Students.sql¡A½Ð±z±NÀɮ׽ƻs¨ì±zªºµwºÐ¸Ì¨Ã¨Ï¥ÎphpMyAdmin¬°¤u¨ã±N¨ä¸ü¤J¨ìMySQL Server¤¤¡A¥H¤è«K±z¾Þ§@½d¨Ò®É¨Ï¥Î¡C
students.sql (77.25 KB)
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

8-1   ¬d¸ß»P¨ú±o¸ê®Æ
·í§Ú­Ì«Ø¥ß»PMySQL Serverªº³sµ²«á¡A²Ä¤@¥ó¨Æ±¡¤£¬O°¨¤W«æµÛ´£¨ú¸ê®Æ¡A¦Ó¬OÀ³¸Ó¿ï¨ú§Ú­Ì­n¨Ï¥Îªº¸ê®Æ®w¡A­n¿ï¨ú¾Þ§@ªº¸ê®Æ®w¥i¨Ï¥Î¡umysql_select_db()¡v¨ç¼Æ¡G
mysql_select_db() ¨ç¼Æ
  1. int mysql_select_db ( string database_name [, int link_identifier])
½Æ»s¥N½X
mysql_select_db()¨ç¼Æ¨Ï¥Î®É»Ýµ¹©w¨â­Ó°Ñ¼Æ¡G
1¡Bdatabase_name¡G«ü©w§Ú­Ì­n¾Þ§@ªº¸ê®Æ®w¦WºÙ¡C
2¡Blink_identifier¡G³s½u¥N¸¹¡Amysql_select_db()¨ç¼Æ¥u¯à«ü©w¾Þ§@­þ­Ó¸ê®Æ®w¡A¨Ã¨S¦³³sµ²¸ê®Æ®wªº¯à¤O¡A¦]¦¹¡Amysql_select_db()¨ç¼Æ¥²¶·³z¹L¬Y­Ó¤w«Ø¥ß¨Ã§¹¦¨¸ê®Æ®w³sµ²ªº¡u³s½u¥N¸¹¡v¡A³o­Ó¡u³s½u¥N¸¹¡v¬O¥Ñmysql_connect()¨ç¼Æ©Ò«Ø¥ßªº¡A³o­Ó°Ñ¼Æ¥i¥H¤£«ü©w¡A¦bmysql_query()¨ç¼Æ°õ¦æ®É·|¦Û°Ê§ä´M³Ìªñ¥Ñmysql_connect()¨ç¼Æ©Ò«Ø¥ßªº³s½u¡C

¨Ò¦p¡G
  1. <?
  2.     $conn = mysql_connect("localhost", "root", "12345a");
  3.     mysql_select_db("STUDENTS", $conn);
  4. ?>
½Æ»s¥N½X
mysql_connect()¨ç¼Æ¦n¥Îªº¦a¤è¦b©ó¡G±ý³s¤Wªº¸ê®Æ®w¦øªA¾¹°Ñ¼Æ¤º®e¥i¥H¬O§Ú­Ì³]©wªºDNS¡B¸ê®Æ®w¦øªA¾¹¦WºÙ¡A³]¸m¬O¸ê®Æ®w¦øªA¾¹ªºIP¡B¹q¸£¦WºÙ¡A¦p¤W¨Ò¤¤ªº¡ulocalhost¡v¡C¡]IP¦ì§}¡u127.0.0.1¡v»P¡ulocalhost¡v·N¸q¬Û¦P¡A¬Ò¥Nªí¥»¾÷¦øªA¾¹¡^

mysql_select_db()¨ç¼Æªº¦^¶Ç­È¬°¤@¥¬ªL­È¡A­Y¿ï¨ú¸ê®Æ®w¦¨¥\«h¦^¶Çtrue¡A­Y¥¢±Ñ«h¦^¶Çfase¡A¦]¦¹¡A§Ú­Ì¥i§Q¥Îmysql_select_db()¨ç¼Æ¨Ó´úª¾¸ê®Æ®w¬O§_¦s¦b¡A¨Ò¦p¡G
Ex08_01.php
  1. <?
  2.     $conn = mysql_connect("localhost ", "root", "12345a");
  3.     if(@mysql_select_db("STUDENTS", $conn))
  4.      {
  5.       echo "¸ê®Æ®w¦s¦b";
  6.      }
  7.     else
  8.      {
  9.       echo "¸ê®Æ®w¤£¦s¦b";
  10.      }
  11. ?>
½Æ»s¥N½X
¦b¤W¨Ò¤¤¡Gif§PÂ_¦¡¤¤«ç¦³¤@­Ó¡u@¡]¤p¦Ñ¹«¡^¡v²Å¸¹¡H¦]¬°µ§ªÌ§Æ±æ¡G·ímysql_select_db()¨ç¼Æ°õ¦æ¥¢±Ñ®ÉPHP¤£­n¦L¥X¦p¹Ï8-1¤¤PHP¦^À³ªº¿ù»~°T®§¡A¤]´N¬O³o­Ó¡u@¡]¤p¦Ñ¹«¡^¡v²Å¸¹·|ÁôÂÃPHP¦^À³ªº¿ù»~°T®§¡I

¹Ï8-1  ¨t²Î¦^À³ªº¿ù»~°T®§¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¬d¸ß»P¨ú±o¸ê®Æªºmysql_query()¨ç¼Æ
·í¨Ï¥Îmysql_select_db()¨ç¼Æ¿ï¨ú§Ú­Ì­n¨Ï¥Îªº¸ê®Æ®w«á¡A´N¥i¥H¶}©l§Q¥Î¡umysql_query()¡v¨ç¼Æ¨Ó¬d¸ß¨Ã¨ú±o¸ê®Æ®w¤º¬Y­Ó¸ê®Æªíªº¸ê®Æ°O¿ý¡G
mysql_query() ¨ç¼Æ
  1. int mysql_query ( string query [, int link_identifier])
½Æ»s¥N½X
mysql_query()¨ç¼Æ¨Ï¥Î®É»Ýµ¹©w¨â­Ó°Ñ¼Æ¡G

  • query¡G¬d¸ß¦r¦ê¡A¤]´N¬O­n¹ï¸ê®Æ®w¤U¹FªºSQL©R¥O¦r¦ê¡A¨Ò¦p¡uSelect¡v©R¥O¡C
  • link_identifier¡G³s½u¥N¸¹¡A¬Y­Ó¤w«Ø¥ß¨Ã§¹¦¨¸ê®Æ®w³sµ²ªº¡u³s½u¥N¸¹¡v¡A³o­Ó°Ñ¼Æ¥i¥H¤£«ü©w¡A¦bmysql_query()¨ç¼Æ°õ¦æ®É·|¦Û°Ê§ä´M³Ìªñ¥Ñmysql_connect()¨ç¼Æ©Ò«Ø¥ßªº³s½u¡C
¨Ò¦p¡G
Ex08_02.php
  1. <?
  2.     $conn = mysql_connect("127.0.0.1", "root", "12345a");
  3.     if(mysql_select_db("STUDENTS", $conn))
  4.      {
  5.       //¸ê®Æ®w¦s¦b,¶i¦æ¸ê®Æ¬d¸ß
  6.       $SQL="SELECT * FROM studentlist";
  7.       mysql_query($SQL);
  8.      }
  9.     else
  10.      {
  11.       echo "¸ê®Æ®w¤£¦s¦b";
  12.      }
  13. ?>
½Æ»s¥N½X
¥Ñmysql_query()¨ç¼Æªº®æ¦¡±z¥i¥Hµo²{¨ä¦^¶Ç­È¬°¤@­Ó¾ã¼Æ­È¡A¨ä¹ê¤£µM¡A¨Ò¦p§Ú­Ì©Ò¤U¹FªºSQL«ü¥O¬°¡uSelect¡v¡]¨ú±o¬Y­Ó¸ê®Æªí¤ºªº¸ê®Æ°O¿ý¡^¡A¦pªG¶¶§Q¨ú¦^¸ê®Æ¡A«h¦^¶Ç­È¨ä¹ê¬O¤@±iµêÀÀªº¡u¸ê®Æªí¡v¡A¤Ï¤§¡A¥¼¶¶§Q¨ú±o¸ê®Æ¦¹®É¦^¶Ç­È¤~·|¬O¾ã¼Æ0¡C
·íµM¡A½d¨Òex08_02¬O¥i¥H°õ¦æªº¡A¦ý«o¥¼¹F¨ì§Ú­Ì´£¨ú¸ê®Æªº¥Øªº¡A¬°¤°»ò©O¡H¦]¬°§Ú­Ì¨S¦³±Nmysql_query()¨ç¼Æ°õ¦æ«áªº¦^¶Çµ²ªG«O¯d¤U¨Ó¡I©Ò¥H§Ú­ÌÀ³¸Ó±N½d¨Òex08_02§ó¥¿¦p¤U¡G
Ex08_03.php
  1. ¡K
  2.     if(@mysql_select_db("STUDENTS", $conn))
  3.      {
  4.       //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  5.       $SQL="SELECT * FROM studentlist";
  6.       //±N¦^¶Çµ²ªG¦s©ñ©óÅܼƤ¤
  7.       $datalist=mysql_query($SQL);
  8.      }
  9.     else
  10.      ¡K
½Æ»s¥N½X
¦ó¿×SQL
SQL¬OStructure Query LanguageªºÂ²¼g¡A½Ķ¬°"µ²ºc¤Æ¬d¸ß»y¨¥"¡CSQL»y¨¥¬O¨Ï¥Î¤@¨Ç²LÅã©öÀ´ªº¤f»y¤Æ¥y¤lºc¦¨«ü¥O¡A¨Ó¦s¨ú¸ê®Æ®wªº¤º®e¡C´N¦]¬°¥¦¤f»y¤Æªº«ü¥O±Ô­z¡A©Ò¥H¦U¤j¼t°Óªº¸ê®Æ®w³nÅé´N¤j¦h¨Ï¥ÎSQL»y¨¥¡A¦Ó·L³nªº¡uSQL Server¡v¥u¬O±o¨ì¤F¡u¦WºÙ¡v¤§«K½}¤F¡A¹³Orcale¡BInterbase©Î§Ú­Ì²{¦b¨Ï¥ÎªºMySQL¸ê®Æ®wµ¥¡A³£¬O¨Ï¥ÎSQL»y¨¥ªº¸ê®Æ®w¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

        ÀR«ä¦Û¦b : ¯àµ½¥Î®É¶¡ªº¤H¡A¥²¯à´x´¤¦Û¤v§V¤Oªº¤è¦V¡C
ªð¦^¦Cªí ¤W¤@¥DÃD