6. ²»ÒªÊ¹ÓÃBY RAND()ÃüÁî
ÕâÊÇÒ»¸öÁîºÜ¶àÐÂÊÖ³ÌÐòÔ±»áµô½øÈ¥µÄÏÝÚå¡£Äã¿ÉÄܲ»Öª²»¾õÖÐÖÆÔìÁËÒ»¸ö¿ÉŵÄƽ¾²¡£Õâ¸öÏÝÚåÔÚÄãÊÇÓÃBY RAND()ÃüÁîʱ¾Í¿ªÊ¼´´½¨ÁË¡£
Èç¹ûÄúÕæµÄÐèÒªËæ»úÏÔʾÄãµÄ½á¹û£¬Óкܶà¸üºÃµÄ;¾¶È¥ÊµÏÖ¡£³ÏÈ»ÕâÐèҪд¸ü¶àµÄ´úÂ룬µ«ÊÇÄܱÜÃâÐÔÄÜÆ¿¾±µÄ³öÏÖ¡£ÎÊÌâÔÚÓÚ£¬MySQL¿ÉÄÜ»áΪ±íÖÐÿһ¸ö¶ÀÁ¢µÄÐÐÖ´ÐÐBY RAND()ÃüÁÕâ»áÏûºÄ´¦ÀíÆ÷µÄ´¦ÀíÄÜÁ¦£©£¬È»ºó¸øÄã½ö½ö·µ»ØÒ»ÐС£
1. // what NOT to do:
2. $r = mysql_query("SELECT username FROM user ORDER BY RAND() LIMIT 1");
3. // much better:
4. $r = mysql_query("SELECT count(*) FROM user");
5. $d = mysql_fetch_row($r);
6. $rand = mt_rand(0,$d[0] - 1);
7.
8. $r = mysql_query("SELECT username FROM user LIMIT $rand, 1");
7. ¾¡Á¿±ÜÃâSELECT *ÃüÁî
´Ó±íÖжÁÈ¡Ô½¶àµÄÊý¾Ý£¬²éѯ»á±äµÃ¸üÂý¡£ËûÔö¼ÓÁË´ÅÅÌÐèÒª²Ù×÷µÄʱ¼ä£¬»¹ÊÇÔÚÊý¾Ý¿â·þÎñÆ÷ÓëWEB·þÎñÆ÷ÊǶÀÁ¢·Ö¿ªµÄÇé¿öÏ¡£Ä㽫»á¾Àú·Ç³£Âþ³¤µÄÍøÂçÑÓ³Ù£¬½ö½öÊÇÒòΪÊý¾Ý²»±ØÒªµÄÔÚ·þÎñÆ÷Ö®¼ä´«Êä¡£
ʼÖÕÖ¸¶¨ÄãÐèÒªµÄÁУ¬ÕâÊÇÒ»¸ö·Ç³£Á¼ºÃµÄÏ°¹ß¡£
1. // not preferred
2. $r = mysql_query("SELECT * FROM user WHERE user_id = 1");
3. $d = mysql_fetch_assoc($r);
4. echo "Welcome {$d['username']}";
5. // better:
6. $r = mysql_query("SELECT username FROM user WHERE user_id = 1");
7. $d = mysql_fetch_assoc($r);
8. echo "Welcome {$d['username']}";
9. // the differences are more significant with bigger result sets
8. ´ÓPROCEDURE ANALYSE()ÖлñµÃ½¨Òé
PROCEDURE ANALYSE()¿ÉÈÃMySQLµÄÖù½á¹¹·ÖÎöºÍ±íÖеÄʵ¼ÊÊý¾ÝÀ´¸øÄãһЩ½¨Òé¡£Èç¹ûÄãµÄ±íÖÐÒѾ´æÔÚʵ¼ÊÊý¾ÝÁË£¬ÄÜΪÄãµÄÖØ´ó¾ö²ß·þÎñ¡£
9. ×¼±¸ºÃµÄÓï¾ä
×¼±¸ºÃµÄÓï¾ä£¬¿ÉÒÔ´ÓÐÔÄÜÓÅ»¯ºÍ°²È«Á½·½Ãæ¶Ô´ó¼ÒÓÐËù°ïÖú¡£
×¼±¸ºÃµÄÓï¾äÔÚ¹ýÂËÒѾ°ó¶¨µÄ±äÁ¿Ä¬ÈÏÇé¿öÏ£¬ÄܸøÓ¦ÓóÌÐòÒÔÓÐЧµÄ±£»¤£¬·ÀÖ¹SQL×¢Èë¹¥»÷¡£µ±È»ÄãÒ²¿ÉÒÔÊÖ¶¯¹ýÂË£¬²»¹ýÓÉÓÚ´ó¶àÊý³ÌÐòÔ±½¡ÍüµÄÐÔ¸ñ£¬ºÜÄѴﵽЧ¹û¡£
1. // create a prepared statement
2. if ($stmt = $mysqli->prepare("SELECT username FROM user WHERE state=?")) {
3. // bind parameters
4. $stmt->bind_param("s", $state);
5. // execute
6. $stmt->execute();
7. // bind result variables
8. $stmt->bind_result($username);
9. // fetch value
10. $stmt->fetch();
11. printf("%s is from %s\n", $username, $state);
12. $stmt->close();
13. }
10. ½«IPµØÖ·´æ´¢ÎªÎÞ·ûºÅÕûÐÍ
Ðí¶à³ÌÐòÔ±ÔÚ´´½¨Ò»¸öVARCHAR£¨15£©Ê±²¢Ã»ÓÐÒâʶµ½ËûÃÇ¿ÉÒÔ½«IPµØÖ·ÒÔÕûÊýÐÎʽÀ´´æ´¢¡£µ±ÄãÓÐÒ»¸öINTÀàÐÍʱ£¬ÄãÖ»Õ¼ÓÃ4¸ö×ֽڵĿռ䣬ÕâÊÇÒ»¸ö¹Ì¶¨´óСµÄÁìÓò¡£
Äã±ØÐëÈ·¶¨ÄãËù²Ù×÷µÄÁÐÊÇÒ»¸öUNSIGNED INTÀàÐ͵Ä,ÒòΪIPµØÖ·½«Ê¹ÓÃ32λunsigned integer¡£
1. $r = "UPDATE users SET ip = INET_ATON('{$_SERVER['REMOTE_ADDR']}') WHERE user_id = $user_id";
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ