ºìÁªLinuxÃÅ»§
Linux°ïÖú

PHP·ÃÎÊMySQLƪ

·¢²¼Ê±¼ä:2006-02-10 09:33:17À´Ô´:ºìÁª×÷Õß:wide
ÔÚÕâÒ»ÕÂÄÚÎÒÃÇ»áѧϰµ½ÈçºÎÔÚÒ»¸öWebÒ³ÃæÖÐÏòÊý¾Ý¿âÖд洢ÐÅÏ¢²¢ÏÔʾËü¡£Ö®Ç°ÎÒÃÇÒѾ­°²×°ÁËMySQLÕâ¸ö¹ØϵÐÍÊý¾Ý¿âÒýÇæÒÔ¼°PHPÕâ¸ö·þÎñÆ÷¶Ë½Å±¾ÓïÑÔ£¬²¢Ñ§Ï°ÁËÓйØËüÃǵĻù±¾ÖªÊ¶¡£ÔÚѧÍêÕâÒ»Õºó£¬ÎÒÃǽ«Ã÷°×ÈçºÎ×ÛºÏÀûÓÃÕâÁ½¸öÐµĹ¤¾ßÀ´¹¹½¨Ò»¸öÊý¾Ý¿âÇý¶¯µÄÍøÕ¾£¡

¶ÔÇ°Ò»²¿·ÖµÄ»Ø¹Ë
ÔÚÎÒÃÇÍùϼÌÐø֮ǰ£¬»Ø¹ËÒ»ÏÂÎÒÃÇѧϰµÄÄ¿µÄÓ¦¸ÃÊǼþÓмÛÖµµÄÊ¡£ÏÖÔÚÓÐÎÒÃǵÄϵͳÖÐÓÐÁËÁ½¸öÇ¿ÓÐÁ¦µÄÐµĹ¤¾ß£º½Å±¾ÓïÑÔPHPºÍÊý¾Ý¿âÒýÇæMySQL¡£¸ãÇå³þÁ½ÕßÊÇÈç¹ûЭͬ¹¤×÷ÊǺÜÖØÒªµÄ¡£

Êý¾Ý¿âÇý¶¯µÄÍøÕ¾µÄʵÖʾÍÊÇÔÊÐíÕ¾µãµÄÄÚÈÝ´æÔÚÓÚÒ»¸öÊý¾Ý¿âÖУ¬²¢ÇÒ¿ÉÒÔͨ¹ýÕâ¸öÊý¾Ý¿âÀ´¶¯Ì¬µØ²úÉúWebÒ³ÃæÀ´ÈÃÎÒÃǵķÃÎÊÕßͨ¹ý±ê×¼µÄWebä¯ÀÀÆ÷À´ÏÔʾËü¡£ËùÒÔÔÚÄãµÄϵͳµÄÒ»¶ËÊÇÒ»¸ö·ÃÎÊÄãµÄÕ¾µãµÄä¯ÀÀÕߣ¬Ëûͨ¹ý·ÃÎÊhttp://www.yoursite.com/À´»ñµÃÒ»¸ö±ê×¼µÄHTML¸ñʽµÄWebÒ³Ãæ²¢ÔÚWebä¯ÀÀÆ÷ÖÐÏÔʾËü¡£ÔÚÄãµÄϵͳµÄÁíÒ»¶ËÊÇͨ¹ýÒ»¸ö»ò¼¸¸öÊý¾Ý±í´æ´¢ÔÚÒ»¸öÖ»Àí½âÈçºÎÏìÓ¦SQL²éѯ£¨ÃüÁµÄMySQLÊý¾Ý¿âÖеÄÄãµÄÕ¾µãµÄÄÚÈÝ¡£

PHP½Å±¾ÓïÑԳе£ÁËÁ½ÕßÖ®¼äµÄÁªÂçÔ±µÄ½ÇÉ«£¬Ê¹ÓÃPHP£¬Äã¿ÉÒÔ±àдһ¸ö±ê×¼HTMLµÄ¡°Ä£°å¡±£¬Õâ¸ö¡°Ä£°å¡±¾ö¶¨ÁËÄãµÄÕ¾µãµÄÍâ¹Û£¨°üÀ¨Í¼»­ºÍÒ³ÃæÉè¼Æ£©¡£ÕâʱÄÚÈÝÊÇÊôÓÚÕâ¸ö¡°Ä£°å¡±µÄ£¬Äã¿ÉÒÔʹÓÃһЩPHP´úÂëÀ´Á¬½ÓMySQLÊý¾Ý¿â²¢ÇÒʹÓÃSQL²éѯÀ´»ñµÃÊý¾Ý²¢ÔÚÆäÏàӦλÖÃÏÔʾËü£¬ÕâÀïµÄSQL²éѯÊǺÍÎÒÃÇÔÚµÚ¶þÕÂÖÐÓÃÀ´½¨Á¢Ð¦»°Êý¾Ý±íʱһÑùµÄ¡£

ÏÖÔÚ¶ÔÓÚ·ÃÎÊÕßÔÚ·ÃÎÊÄãµÄÊý¾Ý¿âÇý¶¯µÄÍøÕ¾µÄÒ»¸öÒ³Ãæʱ£¬µ½µ×»á·¢ÉúʲôÊ£¬ÄãÓ¦¸ÃÓиöÃ÷È·µÄÈÏʶÁË£º
·ÃÎÊÕßµÄWebä¯ÀÀÆ÷ʹÓÃÒ»¸ö±ê×¼µÄURLÇëÇóÕâ¸öÒ³Ãæ¡£
Web·þÎñÆ÷Èí¼þ£¨Apache¡¢IIS»òÆäËû£©È϶¨±»ÇëÇóµÄÒ³ÃæÊÇÒ»¸öPHP½Å±¾£¬Òò¶øÔÚÏìÓ¦Õâ¸öÒ³ÃæÇëÇó֮ǰÓÃËüµÄPHP²å¼þÀ´½âÊÍËü¡£

һЩPHPÃüÁÎÒÃÇ»¹Ã»Ñ§µ½£©»áÁ¬½ÓMySQLÊý¾Ý¿â²¢ÏòÊý¾Ý¿âÇëÇóÊôÓÚÕâ¸öWebÒ³ÃæµÄÄÚÈÝ¡£
MySQLÊý¾Ý¿â×÷³öÏìÓ¦²¢ÇÒÏòPHP½Å±¾·¢³ö±»ÇëÇóµÄÄÚÈÝ¡£

PHP½Å±¾½«ÄÚÈÝ´æ´¢µ½Ò»¸ö»ò¼¸¸öPHP±äÁ¿ÖУ¬²¢Ê¹ÓÃÎÒÃÇÊìϤµÄechoº¯Êý½«Æä×÷ΪWebÒ³ÃæµÄÒ»²¿·ÖÊä³ö¡£
PHP²å¼þÍê³É´¦Àí²¢½«Éú³ÉµÄHTML¸±±¾·µ»Øµ½Web·þÎñÆ÷¡£

Web·þÎñÆ÷½«Õâ¸öHTML¸±±¾·¢Ë͵½Webä¯ÀÀÆ÷£¬Õ⽫ÊÇÒ»¸ö±ê×¼µÄHTMLÎļþ£¬Ö»²»¹ýËü²»ÊÇÖ±½ÓÀ´×ÔÓÚÒ»¸öHTMLÎļþ£¬¶øÊÇÀ´×ÔÓÚPHP²å¼þÌṩµÄÊä³ö¡£

ÓÃPHPÁ¬½ÓMySQL
ÔÚÎÒÃÇ´ÓÎÒÃǵÄMySQLÊý¾Ý¿âÖлñÈ¡ÎÒÃǵÄWebÒ³ÃæËù°üº¬µÄÄÚÈÝ֮ǰ£¬ÎÒÃÇÊ×ÏȱØÐëÖªµÀÈçºÎ½¨Á¢ÓëMySQLµÄÁ¬½Ó¡£ÔÚµÚ¶þÕÂÖУ¬ÎÒÃÇʹÓÃÁËÒ»¸ö½ÐmysqlµÄ³ÌÐòÀ´×öÕâÑùµÄÁ¬½Ó¡£PHP²»ÐèÒªÕâÑùµÄÒ»¸ö³ÌÐò£¬¶ÔÁ¬½ÓMySQLµÄÖ§³ÖÊÇÓïÑÔÄÚÖõġ£ÏÂÃæµÄÕâ¸öº¯ÊýÓÃÀ´½¨Á¢ÕâÑùµÄÁ¬½Ó£º

ÒýÓÃ:
mysql_connect(
, , );


ÔÚÕâÀ
ÊÇMySQL·þÎñÈí¼þÔÚÆäÉÏÔËÐеļÆËã»úµÄIPµØÖ·»òÖ÷»úÃû£¨Èç¹ûÕâÓëÔËÐÐWeb·þÎñÈí¼þµÄ¼ÆËã»úÊÇͬһ̨£¬Äã¿ÉÒÔʹÓÃ"localhost"£©£¬ºÍ¾ÍÊÇÄãÔÚµÚ¶þÕÂÖÐÓÃÀ´Á¬½Óµ½MySQL·þÎñÆ÷µÄÓû§Ãû¼°¿ÚÁî¡£

Äã¿ÉÄÜ»¹¼ÇµÃPHPÖеĺ¯ÊýÔÚ±»µ÷ÓÃʱÍùÍù»á·µ»Ø£¨Êä³ö£©Ò»¸öÖµ¡£Çë²»Òªµ£ÐÄÎÒÃÇûÓÐÌáÐÑÄ㣬ÎÒÃÇÔÚ×î³õ½Ó´¥Ò»¸öº¯Êýʱ¶¼»áΪÄãÏêϸÏêϸËü¡£¾ø´ó¶àÊýµÄº¯ÊýÔÚ±»µ÷Óú󣬶¼»á·µ»ØÒ»¸ö¿ÉÒÔÔÚ´æ´¢ÔÚ±äÁ¿ÖеÄÖµÒÔ±¸Ï´ÎʹÓá£ÀýÈçÎÒÃÇÉÏÃæ½éÉܵÄmysql_connectº¯Êý£¬»á·µ»ØÒ»¸öÊý×ÖÀ´±êʶÒѾ­½¨Á¢µÄÁ¬½Ó¡£ÒòΪÎÒÃÇ»áҪʹÓÃÕâ¸öÁ¬½Ó£¬ËùÒÔÎÒÃDZØÐë±£´æÕâ¸öÖµ¡£ÏÂÃæÊÇÒ»¸ö¹ØÓÚÈçºÎÁ¬½ÓÎÒÃǵÄMySQLÊý¾Ý¿âµÄÒ»¸öʵÀý£º
$dbcnx = mysql_connect("localhost", "root", "mypasswd");

ÐèҪ˵Ã÷µÄÊÇ£¬¶ÔÓÚÄãµÄMySQL·þÎñÆ÷£¬ÉÏÃæÕâ¸öº¯ÊýÖеÄÈý¸ö²ÎÊýµÄÖµ¿ÉÄÜÊDz»Í¬µÄ¡£ÄãÓ¦¸Ã×¢Òâµ½ÔÚÕâ¶ùÎÒÃǵÄmysql_connect ·µ»ØÁËÒ»¸öÖµ£¨ÎÒÃdzÆ֮Ϊһ¸öÁ¬½Ó±êʶ£©£¬Õâ¸öÖµ±»ÎÒÃÇ´æ´¢ÔÚ±äÁ¿$dbcnxÖС£
ÒòΪMySQLÊÇÒ»¸öÍêÈ«·Ö²¼Ê½µÄÈí¼þ£¬ÎÒÃDZØÐ뿼Âǵ½ÕâЩ¿ÉÄÜÐÔ£º·þÎñ²»¿ÉÓá¢ÍøÂç¶ÂÈû»òÕßÊÇÎÒÃǵÄÓû§Ãû¼°¿ÚÁƥÅä¡£ÔÚÕâЩÇé¿öÏ£¬mysql_connectº¯Êý²»ÄÜ·µ»ØÒ»¸öÁ¬½Ó±êʶ£¨ÒòΪÁ¬½Óδ±»½¨Á¢£©¡£Õâʱ£¬»á·µ»ØÒ»¸öÂß¼­¼Ù¡£ÕâʹµÃÎÒÃÇ¿ÉÒÔÓÃÒ»¸öifÓï¾äÀ´´¦ÀíÁ¬½ÓµÄÇé¿ö£º $dbcnx = @mysql_connect("localhost", "root", "mypasswd");
if (!$dbcnx) {
echo( "

Unable to connect to the " .
"database server at this time.

" );
exit();
}


ÔÚÉÏÃæµÄ´úÂë¶ÎÖгöÏÖÁËÈý¸öеĶ«Î÷£¬Ê×ÏÈ£¬ÎÒÃÇÔÚmysql_connectº¯ÊýÇ°¼ÓÁËÒ»¸ö@·ûºÅ¡£°üÀ¨mysql_connectÔÚÄÚµÄÐí¶àº¯Êý»áÔÚʧ°ÜºóÏÔʾÄÑ¿´µÄ´íÎóÐÅÏ¢¡£ÔÚº¯ÊýÃûÇ°¼ÓÒ»¸ö@·ûºÅ¿ÉÒÔ¸æËßÕâ¸öº¯Êýµ±Ö´ÐÐʧ°Üʱ£¬ÔÊÐíÎÒÃÇÏÔʾÎÒÃÇ×Ô¼ºÓѺõijö´íÐÅÏ¢¡£
Æä´Î£¬ÔÚÎÒÃǵÄifÓï¾äµÄÌõ¼þÖУ¬$dbcnx±äÁ¿Ç°Ãæ¼ÓÁËÒ»¸ö¾ªÌ¾ºÅ¡£Õâ¸ö¾ªÌ¾ºÅÊÇPHPÖеġ°·ñÔËËã·û¡±¡£Ò²¾ÍÊÇ˵½«Âß¼­Õæ±äΪÂß¼­¼Ù£¬½«Âß¼­¼Ù±äΪÂß¼­Õæ¡£ÕâÑù£¬Èç¹ûÕâ¸öÁ¬½ÓÊÇʧ°ÜµÄ£¬mysql_connect»á·µ»ØÒ»¸öÂß¼­¼Ù£¬!$dbcnx½«µÈÓÚÂß¼­Õ棬ÕâÑùÎÒÃǵÄifÓï¾ä½«±»Ö´ÐС£Ïà·´£¬Èç¹ûÕâ¸öÁ¬½ÓÊdzɹ¦µÄ£¬´æ´¢ÔÚ$dbcnx ÖеÄÁ¬½Ó±êʶ½«µÈÓÚÂß¼­Õ棨ÔÚPHPÖУ¬ÈκηÇÁãµÄÊý×Ö¶¼±»ÈÏΪÊÇÂß¼­Õ棩£¬ËùÒÔ!$dbcnx»áµÈÓÚÂß¼­¼Ù£¬ifÓï¾ä½«²»»á±»Ö´ÐС£
×îºóÒ»¸öÊÇexitº¯Êý£¬ÕâÊÇÎÒÃÇÓöµ½µÄµÚÒ»¸öûÓвÎÊýµÄº¯Êý¡£Õâ¸öº¯ÊýµÄÈ«²¿×÷ÓþÍÊǵ¼ÖÂPHPÍ£Ö¹¶Ô±¾Ò³µÄÔĶÁ¡£Èç¹ûÊý¾Ý¿âÁ¬½Óʧ°ÜÕâÊÇÒ»¸öºÜºÃµÄÏìÓ¦£¬ÒòΪ¾ø´ó¶àÊýÇé¿öÏ£¬Èç¹û²»ÄÜÁ¬½Óµ½Êý¾Ý¿â£¬ÕâÒ»Ò³²»»áÏÔʾÈκÎÓÐÓõÄÐÅÏ¢¡£
ºÍÎÒÃÇÔÚµÚ¶þÕÂ×ö¹ýµÄÒ»Ñù£¬Á¬½Ó±»½¨Á¢ºóÏÂÒ»²½¾ÍÊÇÑ¡Ôñ¹¤×÷µÄÊý¾Ý¿â¡£ÎÒÃǽ«ÒªÔÚµÚ¶þÕÂÖÐËù½¨Á¢µÄЦ»°Êý¾Ý¿âÖй¤×÷¡£Õâ¸öÊý¾Ý¿â±»ÃüÃûΪjokes¡£ÔÚPHPÖÐÓÃÀ´Ñ¡ÔñÊý¾Ý¿âµÄÁíÍâÒ»¸öº¯Êý£º
mysql_select_db("jokes", $dbcnx);

Çë×¢ÒâÎÒÃÇÔÚÕâ¶ùʹÓÃÁË$dbcnx±äÁ¿À´Ö¸³öÁËÕâ¸öº¯ÊýËùʹÓõÄÁ¬½Ó±êʶ¡£Õâ¸ö²ÎÊýʵ¼ÊÉÏÊÇ¿ÉÊ¡ÂԵġ£µ±Ê¡ÂÔÕâ¸ö²ÎÊýʱ£¬º¯Êý»á×Ô¶¯Ê¹ÓÃ×îºó¿ªÆôµÄÄÇÒ»¸öÁ¬½Ó¡£Õâ¸öº¯Êý³É¹¦ºó·µ»ØÂß¼­Õ棬ʧ°Üºó·µ»ØÂß¼­¼Ù¡£ÎªÁ˽÷É÷Æð¼û£¬ÎÒÃÇÒ²ÓÃÁËÒ»¸öifÓï¾äÀ´´¦Àí´íÎó£º
ÒýÓÃ:
if (! @mysql_select_db("jokes") ) {
echo( "

Unable to locate the joke " .
"database at this time.

" );
exit();
}



µ±½¨Á¢ÁËÁ¬½Ó²¢Ñ¡ÔñÁËÊý¾Ý¿âÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ¿ªÊ¼Ê¹Óô洢ÔÚÊý¾Ý¿âÖеÄÊý¾ÝÁË¡£

ÔÚPHPÖÐÖ´ÐÐSQL²éѯ
ÔÚµÚ¶þÕÂÖУ¬ÎÒÃÇʹÓÃÒ»¸ö½ÐmysqlµÄ³ÌÐòÀ´Á¬½Óµ½MySQLÊý¾Ý¿â·þÎñÆ÷£¬ÔÚÕâ¸ö³ÌÐòÖУ¬ÎÒÃÇ¿ÉÒÔÊäÈëSQL²éѯ£¨ÃüÁ²¢Á¢¼´ÏÔʾ²éѯ½á¹û¡£ÔÚPHPÖУ¬ÓÐ×ÅÀàËƵĻúÖÆ£ºmysql_queryº¯Êý¡£
mysql_query(, );

ÔÚÕâ¶ù£¬ÊÇÒ»¸ö°üº¬½«Ö´ÐеÄSQLÃüÁîµÄ×Ö·û´®¡£ºÍmysql_select_dbÒ»Ñù£¬Á¬½Ó±êʶÕâ¸ö²ÎÊýÒ²ÊÇ¿ÉÑ¡µÄ¡£
Õâ¸öº¯ÊýµÄ·µ»Ø¾ö¶¨ÓÚ·¢³öµÄ²éѯµÄÀàÐÍ¡£¶ÔÓÚ¾ø´ó¶àÊýµÄSQLÃüÁîÀ´Ëµ£¬mysql_query·µ»ØÂß¼­Õæ»òÂß¼­¼ÙÀ´±êÃ÷Ö´ÐÐÊÇ·ñ³É¹¦¡£Çë²Î¿´ÏÂÃæÕâ¸öÀý×Ó£¬ÕâÊÇÓÃÀ´½¨Á¢ÎÒÃÇÔÚµÚ¶þÕÂÖн¨Á¢µÄJokesÊý¾Ý±íµÄ£º
ÒýÓÃ:
$sql = "CREATE TABLE Jokes ( " .
"ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " .
"JokeText TEXT, " .
"JokeDate DATE NOT NULL " .
")";
if ( mysql_query($sql) ) {
echo("

Jokes table successfully created!

");
} else {
echo("

Error creating Jokes table: " .
mysql_error() . "

");
}



Õâ¶ùʹÓõÄmysql_error½«ÒÔ×Ö·û´®µÄÐÎʽ·µ»ØMySQL·þÎñÆ÷×îºó·¢³öµÄ´íÎóÐÅÏ¢¡£
¶ÔÓÚDELETE¡¢ INSERTÒÔ¼°UPDATE£¨ÓÃÀ´Ð޸Ĵ洢µÄÊý¾Ý£©£¬MySQL¿ÉÒÔÖªµÀÓжàÉÙÊý¾ÝÐб»Õâ¸ö²éѯӰÏì¡£²Î¿´ÏÂÃæµÄSQLÃüÁÕâ¸öÃüÁîÎÒÃÇÔøÔÚµÚ¶þÕÂÖÐÓÃÀ´ÉèÖÃËùÓаüº¬µ¥´Ê¡°chicken¡±µÄЦ»°µÄÈÕÆÚ£º
$sql = "UPDATE Jokes SET JokeDate='1990-04-01' " .
"WHERE JokeText LIKE '%chicken%'";
µ±ÎÒÃÇÖ´ÐÐÕâ¸ö²éѯʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃmysql_affected_rowsº¯ÊýÀ´ÏÔʾÕâ¸öÐÞ¸ÄËùÓ°ÏìµÄÊý¾ÝÐеÄÊýÄ¿£º
ÒýÓÃ:
if ( mysql_query($sql) ) {
echo("

Update affected " .
mysql_affected_rows() . " rows.

");
} else {
echo("

Error performing update: " .
mysql_error() . "

");
}



SELECTÃüÁî»áÓÐһЩ²»Í¬£¬ÒòΪËü»áµÃµ½Ðí¶àÐÅÏ¢£¬¶øPHP±ØÐëÌṩ·½·¨À´´¦ÀíÕâЩÐÅÏ¢¡£

´¦ÀíSELECT½á¹û¼¯
¶ÔÓÚ¾ø´ó¶àÊýµÄSQL²éѯÀ´Ëµ£¬mysql_queryº¯Êý½ö½ö·µ»ØÂß¼­Õæ»òÂß¼­¼Ù¡£¶ø¶ÔÓÚSELECT²éѯÀ´Ëµ£¬½ö½öÕâÑùÏÔÈ»ÊDz»¹»µÄ¡£ÄãÓ¦¸Ã»¹¼ÇµÃSELECT²éѯÊÇÓÃÀ´ÏÔʾÊý¾Ý¿âÖд洢µÄÊý¾ÝµÄ¡£³ýÁËÖ¸³ö²éѯ³É¹¦»¹ÊÇʧ°ÜÖ®Í⣬PHP»¹±ØÐëµÃµ½²éѯµÄ½á¹û¡£×÷Ϊһ¸ö½á¹û£¬µ±ÎÒÃÇÖ´ÐÐÒ»¸öSELECT²éѯµÄʱºò£¬mysql_query»á·µ»ØÒ»¸ö±êʶ¡°½á¹û¼¯¡±µÄÊý×Ö£¬Õ⽫°üº¬ÁËÕâ¸ö²éѯ·µ»ØµÄËùÓÐÐеÄÁÐ±í¡£Èç¹û²éѯʧ°Ü£¬º¯ÊýÈÔÈ»ÊÇ·µ»ØÒ»¸öÂß¼­¼Ù¡£
$result = mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("

Error performing query: " .
mysql_error() . "

");
exit();
}


¼Ù¶¨ÔÚÖ´ÐвéѯʱûÓÐÓöµ½´íÎó£¬ÉÏÃæµÄ´úÂë»á¶¨Î»Ò»¸öÓйش洢ÔÚЦ»°¿âÖÐËùÓÐЦ»°µÄÕýÎĵĽá¹û¼¯£¬Õâ¸ö¶¨Î»±»´æ´¢ÔÚ±äÁ¿$resultÖС£ÒòΪÊý¾Ý¿âÖеÄЦ»°µÄÊýÄ¿ÊÇûÓÐÏÞÖƵģ¬Õâ¸ö½á¹û¼¯¿ÉÄܷdz£ÅÓ´ó¡£
ÎÒÃÇ֮ǰÔø¾­Ìáµ½¹ýwhileÑ­»·¶ÔÓÚ´¦Àí´óÁ¿µÄÊý¾ÝÀ´ËµÊÇÒ»¸ö·Ç³£ÓÐÓõĿØÖÆÓï¾ä£¬ÕâÊÇÎÒÃÇÖð¸ö´¦Àí½á¹û¼¯ÖÐÊý¾ÝÐеĴúÂëµÄ»ù±¾¸ñʽ£º
while ( $row = mysql_fetch_array($result) ) {
// process the row...
}


ÔÚÕâ¸öwhileÑ­»·ÖеÄÌõ¼þ¿ÉÄÜ¿´ÉÏÈ¥ÓëÎÒÃÇÔø¾­Ê¹ÓùýµÄÓÐËù²»Í¬£¬ËùÒÔÎÒÃÇÓбØÒªÔÚÕâÀï½âÊÍËüµÄ¹¤×÷»úÀí¡£Äã¿ÉÒÔÏÈ°ÑÕâ¸öÌõ¼þ¿´³ÉÒ»¸ö¶ÀÁ¢µÄÓï¾ä£º
$row = mysql_fetch_array($result);

mysql_fetch_arrayº¯ÊýÒÔÒ»¸ö²ÎÊý£¨¶ÔÓÚÕâ¸öÀý×ÓÀ´ËµÊÇ´æ´¢ÔÚ$result±äÁ¿ÖУ©½ÓÊܵ½Ò»¸ö½á¹û¼¯£¬²¢ÒÔÒ»¸öÊý¾ÝµÄÐÎʽ·µ»Ø½á¹û¼¯ÖеÄÏÂÒ»ÐС£Èç¹ûÄ㻹²»ÊìϤÊý×éµÄ¸ÅÄ²»Òªµ£ÐÄ£¬ÎÒÃÇ»áÔÚÏÂÃæÏêϸÌÖÂÛËü¡£Èç¹ûÔÚÕâ¸ö½á¹û¼¯Öв»ÔÙÓÐÆäËüÊý¾ÝÐÐʱ£¬mysql_fetch_array·µ»ØÂß¼­¼Ù¡£
ÏÖÔÚ£¬ÎÒÃÇÉÏÃæµÄÓï¾ä¶¨ÒåÁËÒ»¸öÖµµ½$row±äÁ¿ÖУ¬Óë´Ëͬʱ£¬Õû¸öÓï¾äÒ²»ñµÃÁËͬÑùµÄÖµ¡£Õâ¾ÍÊÇÎÒÃÇÔÚwhileÑ­»·µÄÌõ¼þÖÐʹÓÃÕâ¸öÓï¾äµÄÔ­Òò£¬ÒòΪwhileÑ­»·»áÒ»Ö±Ö´ÐÐÑ­»·Ö±µ½Ìõ¼þµÈÓÚÂß¼­¼Ù£¬½á¹û¼¯Óм¸ÐУ¬Õâ¸öÑ­»·¾Í»áÖ´Ðм¸´Î£¬Ã¿Ò»´Î$row¶¼»áµÃµ½Ò»¸öÏÂÒ»ÐеÄÖµ£¬ÏÖÔÚʣϵľÍÊÇÈçºÎÔÚÑ­»·ÖдÓ$row±äÁ¿ÖлñµÃÏàÓ¦µÄÖµÁË¡£
½á¹û¼¯ÖеÄÐб»ÃèÊö³ÉÒ»¸öÊý×é¡£Êý×éÊÇÒ»¸öÌØÊâÀàÐ͵ıäÁ¿£¬Õâ¸ö±äÁ¿¿ÉÒÔ°üº¬¶à¸öÖµ£¬Èç¹ûÄã°ÑÒ»¸ö±äÁ¿¿´³ÉÊÇÖµµÄÈÝÆ÷£¬Äã¿ÉÒÔ°ÑÊý×é¿´³ÉÊÇÓмä¸ôµÄÈÝÆ÷£¬ÔÚÿһ¸ö¼ä¸ôÖпÉÒÔ´æ´¢Ò»¸öµ¥¶ÀµÄÖµ¡£¶ÔÓÚÎÒÃǵÄÊý¾ÝÐÐÀ´Ëµ£¬Õâ¸ö¼ä¸ôÊÇÒÔÊý¾Ý±íµÄÁÐÃûÃüÃûµÄ¡£Èç¹û$rowÊÇÎÒÃǽá¹û¼¯ÖеÄÒ»ÐУ¬ÄÇô$row["JokeText"]¾ÍÊÇÕâÒ»ÐÐÖÐJokeTextÁеÄÖµ¡£ËùÒÔÈç¹ûÎÒÃÇÏëÒªÏÔʾÎÒÃǵÄÊý¾Ý¿âÖÐËùÔÚЦ»°µÄÕýÎÄ£¬whileÑ­»·Ó¦¸ÃÊÇÕâÑùµÄ£º
while ( $row = mysql_fetch_array($result) ) {
echo("

" . $row["JokeText"] . "

");
}


×îºó£¬×÷Ϊһ¸ö×ܽᣬÕâÊÇÒ»¶ÎÍêÕûµÄPHPµÄWebÒ³ÃæµÄ´úÂ룬ËüÓÃÀ´Á¬½ÓÎÒÃǵÄÊý¾Ý¿â£¬È¡³öÊý¾Ý¿âÖÐËùÓÐЦ»°µÄÕýÎÄ£¬²¢½«ÆäÔÚHTMLÖÐÏÔʾ³öÀ´£º
ÒýÓÃ:


Our List of Jokes
<£¯HEAD>

$dbcnx = @mysql_connect("localhost",
"root", "mypasswd");
if (!$dbcnx) {
echo( "

Unable to connect to the " .
"database server at this time.

" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "

Unable to locate the joke " .
"database at this time.

" );
exit();
}
?>

Here are all the jokes in our database:



// Request the text of all the jokes
$result = mysql_query(
"SELECT JokeText FROM Jokes");
if (!$result) {
echo("

Error performing query: " .
mysql_error() . "

");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("

" . $row["JokeText"] . "

");
}
?>





ÏòÊý¾Ý¿âÖвåÈëÊý¾Ý
ÔÚÕâÒ»½ÚÀÎÒÃǻῴµ½ÎÒÃÇ»áÈçºÎ×ÛºÏÀûÓÃÕâЩ¹¤¾ßÀ´ÈÃÎÒÃÇÕ¾µãµÄ·ÃÎÊÕßÏòÊý¾Ý¿âÖÐÌí¼ÓËûÃÇ×Ô¼ºµÄЦ»°¡£Èç¹ûÄãϲ»¶ÌôÕ½£¬Äã¿ÉÒÔÊÔÊÔÔÚÏòÏ¿´Ö®Ç°ÏëÏë´óÖÂÉÏÓ¦¸ÃÔõô×ö¡£ÔÚÕâÒ»½ÚÀïÖ»ÓкÜÉÙеĶ«Î÷¡£¶ÔÓÚÎÒÃÇѧ¹ýµÄ¶«Î÷À´Ëµ£¬ÕâÖ»ÊÇÒ»¸ö¼òµ¥µÄÓ¦Óá£

Èç¹ûÎÒÃÇÏëÒªÈ÷ÃÎÊÕßÄܹ»ÊäÈëеÄЦ»°£¬ÎÒÃÇÊ×ÏÈÐèÒªÒ»¸ö±íµ¥£¬Õâ¶ùÊÇÕâ¸ö±íµ¥µÄ´úÂ룺
ÒýÓÃ:

Type your joke here:







ÕýÈçÎÒÃÇÉÏÃæ¿´µ½µÄÄÇÑù£¬Õâ¸ö±íµ¥ÔÚÌύʱ»áÔØÈëͬһ¸öÒ³Ã棨ÒòΪÎÒÃÇÔÚ±íµ¥µÄACTIONÊôÐÔÖÐʹÓÃÁË$PHP_SELF±äÁ¿£©£¬µ«ÊÇÔÚÔÙ´ÎÔØÈëʱÇëÇóÖаüº¬ÁËÁ½¸ö±äÁ¿£¬Ê×ÏÈÊÇ$joketext£¬ÕâÊÇÔÚtextÓòÖÐÊäÈëµÄЦ»°µÄÕýÎÄ£¬ÁíÒ»¸öÊÇ$submitjoke£¬Õâ¸ö±äÁ¿µÄÖµ½«Ê¼ÖÕÊÇ"SUBMIT"£¬ÕâÓÃÀ´±ê־Ц»°Òѱ»Ìá½»¡£
Òª½«ÒÑÌá½»µÄЦ»°Ìí¼Óµ½Êý¾Ý¿âÖУ¬ÎÒÃÇÐèÒªÓÃmysql_queryÀ´ÔËÐÐÒ»¸öINSERT²éѯ£¬Õâ¸ö²éѯÖн«°üº¬ÒѾ­Ìá½»µÄ$joketext±äÁ¿µÄÖµ£º
ÒýÓÃ:
if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("

Your joke has been added.

");
} else {
echo("

Error adding submitted joke: " .
mysql_error() . "

");
}
}



ÔÚÈ«²¿µÄÄÚÈÝÖÐÖ»ÓÐSQL´úÂëÖгöÏÖÁËÒ»¸öеĶ«Î÷¡£ÔÚÕâÀïÎÒÃÇʹÓÃÁËÒ»¸öMySQLº¯ÊýCURDATE()À´½«Ð²åÈëÊý¾Ý¿âµÄЦ»°µÄJokeDateÁеÄÖµÖÃΪµ±Ç°ÈÕÆÚ¡£ÊÂʵÉÏ£¬MySQLÓкܶàÕâÑùµÄº¯Êý£¬µ«ÊÇÎÒÃÇÖ»»áÔÚʹÓõ½ËûÃÇʱ²Å»á½éÉÜËûÃÇ£¬ÒªµÃµ½Ò»¸öÍêÕûµÄº¯ÊýµÄ˵Ã÷£¬Äã¿ÉÒԲο´MySQL²Î¿¼Êֲᡣ

ÏÖÔÚÎÒÃÇÒѾ­ÓÐÁËÔÊÐíÓû§ÊäÈëÒ»¸öЦ»°²¢½«Æä¼ÓÈëµ½ÎÒÃǵÄÊý¾Ý¿âÖеijÌÐò´úÂë¡£ÏÖÔÚʣϵľÍÊǽ«Æä¼ÓÈëµ½ÎÒÃÇÒÑ×öºÃµÄЦ»°ÏÔʾҳÃæ¡£ÒòΪ¾ø´ó¶àÊýµÄÓû§Ö»»áÏëÒª¿´¿´Ð¦»°£¬ËùÒÔÎÒÃDz»Ïë¶ÔÎÒÃǵÄÒ³Ãæ×ö´óµÄ¸ü¸Ä£¬³ý·ÇÓû§±íʾÏëÒªÌí¼ÓÒ»¸öеÄЦ»°¡£ÒòΪÕâ¸öÔ­Òò£¬ÎÒÃǵÄÓ¦ÓóÌÐòÓ¦¸ÃÊÇÒ»¸ö¶à¹¦ÄܵÄÒ³Ãæ¡£ÏÂÃæÊdzÌÐòµÄ´úÂ룺
ÒýÓÃ:

...

// If the user wants to add a joke
if (isset($addjoke)):
?>

Type your joke here:





else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost",
"root", "mypasswd");
if (!$dbcnx) {
echo( "

Unable to connect to the " .
"database server at this time.

" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "

Unable to locate the joke " .
"database at this time.

" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("

Your joke has been added.

");
} else {
echo("

Error adding submitted joke: " .
mysql_error() . "

");
}
}
echo("

Here are all the jokes " .
"in our database:

");
// Request the text of all the jokes
$result = mysql_query(
"SELECT JokeText FROM Jokes");
if (!$result) {
echo("

Error performing query: " .
mysql_error() . "

");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("

" . $row["JokeText"] . "

");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("

" .
"Add a Joke!

");
endif;
?>




ÏÖÔÚÎÒÃÇÓÐÁËÒ»¸öµ¥¶ÀµÄÎļþ£¬Õâ¸öÎļþ°üº¬²»Ì«¶àµÄPHP´úÂ룬ͨ¹ýÕâ¸öÎļþ£¬ÎÒÃÇ¿ÉÒÔÏÔʾÎÒÃǵÄMySQLÊý¾Ý¿âÖеÄЦ»°²¢ÄÜÏòÎÒÃǵÄMySQLÊý¾Ý¿âÖÐÌí¼ÓЦ»°¡£

Ò»¸öÌôÕ½
×÷Ϊ¼ÒÍ¥×÷Òµ£¬Äã¿ÉÒÔ¿´¿´ÄãÊDz»ÊÇÄܽâ¾öÕâôһ¸öÎÊÌ⣺ÔÚÒ³ÃæÉÏÏÔʾµÄÿһ¸öЦ»°ºóÃæ·ÅÖÃÒ»¸ö½Ð¡°Delete this Joke¡±µÄ³¬Á¬½Ó£¬µ±µ¥»÷Õâ¸öÁ¬½Óʱ£¬»á´ÓÊý¾Ý¿âÖÐɾ³ýÕâ¸öЦ»°²¢ÏÔʾ¸ü¸Ä¹ýÒÔºóµÄЦ»°ÁÐ±í¡£ÏÂÃæÊǶÔÄãµÄһЩÌáʾ£º

Äã¿ÉÒÔ»¹ÔÚÒ»¸ö¶à¹¦ÄÜÒ³ÃæÍê³ÉÈ«²¿µÄ¹¦ÄÜ¡£
ÄãÐèҪʹÓÃSQLµÄDELETEÃüÁÕâ¸öÃüÁîÎÒÃÇÔøÔÚµÚ¶þÕÂÖÐѧϰ¹ý¡£
ÕâÊÇÒ»¸ö¹Ø¼üµÄÎÊÌ⡣Ҫɾ³ýÒ»¸öÖ¸¶¨µÄÊý¾Ý¿â£¬ÄãÐèÒªÄܹ»Î¨Ò»µØ±êʶËü¡£Jokes±íÖеÄID¿ÉÒÔÍê³ÉÕâ¸ö¹¦ÄÜ¡£Äã±ØÐ뽫Ҫ±»É¾³ýµÄЦ»°µÄID´«µÝµ½É¾³ýЦ»°µÄÇëÇóÖС£½«Õâ¸öÖµ·Åµ½¡°Delete this Joke¡±Á¬½ÓµÄ²éѯ×Ö·û´®ÖÐÊDZȽϺÏÊʵġ£
Èç¹ûÄã¾õµÃÄãÒѾ­ÓÐÁ˴𰸣¬»òÕßÄãÖ»ÏëÖªµÀ½â¾ö·½°¸£¬ÄǾÍÈ¥¿´¿´ÏÂÒ»Ò³¡£×£ÄãºÃÔË£¡

½áÓï
ÔÚÕâÒ»ÕÂÖУ¬ÎÒÃÇѧϰÁËһЩеÄÓÃÀ´ÊµÏÖÓëMySQLÊý¾Ý¿â·þÎñ½Ó¿ÚµÄPHPº¯Êý¡£Ê¹ÓÃÕâЩº¯Êý£¬ÎÒÃǽ¨Á¢ÁËÎÒÃǵĵÚÒ»¸öÊý¾Ý¿âÇý¶¯µÄÍøÕ¾£¬ÎÒÃǵÄÕâ¸öÍøÕ¾¿ÉÒÔÔÚÏߵط¢²¼ÎÒÃÇÊý¾Ý¿âÖÐЦ»°²¢ÔÊÐí·ÃÎÊÕßÏòÆäÖÐÌí¼ÓËûÃÇ×Ô¼ºµÄЦ»°¡£

ÔÚµÚÎåÕÂÖУ¬ÎÒÃÇ»á»Øµ½MySQLÃüÁîÐÐȥѧϰÈçºÎʹÓùØϵÐÍÊý¾Ý¿âµÄÔ­ÀíÒÔ¼°ÆäËûһЩ¸ü¸ß¼¶µÄSQL²éѯȥÃèÊö¸üΪ¸´ÔÓµÄÐÅÏ¢£¬²¢¸øÓè·ÃÎÊÕ߶ÔËûÃÇ×Ô¼ºÌí¼ÓµÄЦ»°ÒÔÌرðµÄȨÏÞ£¡
ÌôÕ½µÄ½â¾ö
ÕâÊÇÎÒÃÇÉÏÃæÌá³öµÄ¡°¼ÒÍ¥×÷Òµ¡±µÄ½â¾ö·½°¸¡£ÒªÔÚÿһ¸öЦ»°ºóÃæÌí¼ÓÒ»¸ö¡°Delete this Joke¡±Á¬½Ó£¬±ØÐë×÷ÏÂÃæµÄ¸Ä¶¯£º

֮ǰ£¬ÎÒÃÇÔø¾­ÔÚÎÒÃǵÄÒ³ÃæµÄµ×¶ËµÄ¡°Add a Joke!¡±Á¬½ÓÖд«µÝ¹ýÒ»¸ö$addjoke±äÁ¿£¬Í¨¹ýÕâ¸ö±äÁ¿À´Í¨ÖªÎÒÃǵĽű¾²»ÔÙÏÔʾͨ³£µÄЦ»°ÁÐ±í£¬¶øÊÇÏÔʾһ¸ö¼ÈëЦ»°µÄ±íµ¥¡£Óë´ËÏàÀàËÆ£¬ÎÒÃÇÔÚÎÒÃǵġ°Delete this Joke¡±Á¬½ÓÖд«µÝÒ»¸ö$deletejoke±äÁ¿À´±íʾÎÒÃÇÏëҪɾ³ýÒ»¸öЦ»°¡£

ÔÚ»ñµÃÿһ¸öЦ»°µÄJokeTextÁеÄͬʱ£¬ÎÒÃÇ»¹»ñµÃÁËIDÁеÄÖµ£¬ËùÒÔÎÒÃÇ»ñµÃÁËÓëÊý¾Ý¿âÖÐÿһ¸öЦ»°¹ØÁªµÄID¡£

ÎÒÃǽ«ÒªÉ¾³ýµÄЦ»°µÄIDÖµ¸³Óè$deletejoke±äÁ¿¡£ÕâÊÇͨ¹ý½«´ÓÊý¾Ý¿âÖлñµÃµÄIDÖµ²åÈ뵽ÿһ¸öЦ»°µÄ¡°Delete this Joke¡±Á¬½ÓÖÐÀ´ÊµÏֵġ£

ʹÓÃÁËÒ»¸öif Óï¾ä£¬Èç¹ûÔÚÔØÈëÕâһҳʱ£¬ÎÒÃǵÄ$deletejoke±»¸³ÓèÁËÒ»¸öÖµ£¨Ê¹ÓÃissetº¯Êý£©£¬ÎÒÃÇ»áÔÚÒ»¸öSQLDELETEÓï¾äÖÐʹÓÃÕâ¸öÖµ£¨½«±»É¾³ýµÄЦ»°µÄID£©À´É¾³ýÖ¸¶¨µÄЦ»°¡£
Õâ¶ùÊÇÈ«²¿µÄÔ´´úÂ룺
ÒýÓÃ:

...

// If the user wants to add a joke
if (isset($addjoke)):
?>

Type your joke here:





else:
// Connect to the database server
$dbcnx = @mysql_connect(
"localhost", "root", "mypasswd");
if (!$dbcnx) {
echo( "

Unable to connect to the " .
"database server at this time.

" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "

Unable to locate the joke " .
"database at this time.

" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("

Your joke has been added.

");
} else {
echo("

Error adding submitted joke: " .
mysql_error() . "

");
}
}
// If a joke has been deleted,
// remove it from the database.
if (isset($deletejoke)) {
$sql = "DELETE FROM Jokes " .
"WHERE ID=$deletejoke";
if (mysql_query($sql)) {
echo("

The joke has been deleted.

");
} else {
echo("

Error deleting joke: " .
mysql_error() . "

");
}
}
echo("

Here are all the jokes " .
"in our database:

");
// Request the ID and text of all the jokes
$result = mysql_query(
"SELECT ID, JokeText FROM Jokes");
if (!$result) {
echo("

Error performing query: " .
mysql_error() . "

");
exit();
}
// Display the text of each joke in a paragraph
// with a "Delete this Joke" link next to each.
while ( $row = mysql_fetch_array($result) ) {
$jokeid = $row["ID"];
$joketext = $row["JokeText"];
echo("

$joketext " .
"" .
"Delete this Joke

");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("

" .
"Add a Joke!

");
endif;
?>

ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ