PHP慣用整理

來源:互聯網
上載者:User
PHP常用整理

1、通過網域名稱取得網站IP地址 gethostbyname('網域名稱');?? 如gethostbyname('www.baidu.com');返回IP地址

ip2long('IP地址')? 將IP地址轉為long資料類型
long2ip('long類型')?? 將long類型轉換為IP地址

?

2、將一張表的資料複製到另外一張表中(兩張表的結構必須一致)

INSERT INTO user_new(t_name,sex) SELECT t_name,sex FROM user_Old

?

3、mysql中分表的建立方法

mysql> CREATE TABLE IF NOT EXISTS `user1` ( -> `id` int(11) NOT NULL AUTO_INCREMENT, -> `name` varchar(50) DEFAULT NULL, -> `sex` int(1) NOT NULL DEFAULT '0', -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; Query OK, 0 rows affected (0.05 sec) mysql> CREATE TABLE IF NOT EXISTS `user2` ( -> `id` int(11) NOT NULL AUTO_INCREMENT, -> `name` varchar(50) DEFAULT NULL, -> `sex` int(1) NOT NULL DEFAULT '0', -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; Query OK, 0 rows affected (0.01 sec) mysql> INSERT INTO `user1` (`name`, `sex`) VALUES('張映', 0); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO `user2` (`name`, `sex`) VALUES('tank', 1); Query OK, 1 row affected (0.00 sec) mysql> CREATE TABLE IF NOT EXISTS `alluser` ( -> `id` int(11) NOT NULL AUTO_INCREMENT, -> `name` varchar(50) DEFAULT NULL, -> `sex` int(1) NOT NULL DEFAULT '0', -> INDEX(id) -> ) TYPE=MRG_MyISAM UNION=(user1,user2) INSERT_METHOD=LAST AUTO_INCREMENT=1 ; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select id,name,sex from alluser; +----+--------+-----+ | id | name | sex | +----+--------+-----+ | 1 | 張映 | 0 | | 1 | tank | 1 | +----+--------+-----+ 2 rows in set (0.00 sec) mysql> INSERT INTO `alluser` (`name`, `sex`) VALUES('tank2', 0); Query OK, 1 row affected (0.00 sec) mysql> select id,name,sex from user2 -> ; +----+-------+-----+ | id | name | sex | +----+-------+-----+ | 1 | tank | 1 | | 2 | tank2 | 0 | +----+-------+-----+ 2 rows in set (0.00 sec)

?


?

4、最佳化limit和offset
MySQL的limit工作原理就是先讀取n條記錄,然後拋棄前n條,讀m條想要的,所以n越大,效能會越差。
最佳化前SQL: SELECT * FROM member ORDER BY last_active LIMIT 50,5
最佳化後SQL: select * from zb_sms_sendsmslog inner join (select id from zb_sms_sendsmslog order by id limit 100000,100) as tmp using(id)

分別在於,最佳化前的SQL需要更多I/O浪費,因為先讀索引,再讀資料,然後拋棄無需的行。而最佳化後的SQL(子查詢那條)唯讀索引(Cover index)就可以了,然後通過member_id讀取需要的列。

?

5、POST提交表單後的內容中的雙引號全部都自動在前面被加上了反斜線

$b_Str = $_POST[$b_ControlName]; if (get_magic_quotes_gpc()) { $b_Str = stripslashes($b_Str); }


?

6、Php使用GBK編碼時,參數中帶有特殊中文字元如(縗)

在執行Sql前先執行

SET character_set_connection=GBK, character_set_results=GBK,character_set_client=binary

?

?

?7、Smarty模板中定義變數

<{assign var="i" value=0}> <{section name=book loop=$books}>

<{$i=$i+1}><{$i}>. Title:<{$books[book].title}>
Author:<{$books[book].author}>
ISBN:<{$books[book].isbn}>
Price:<{$books[book].price}>
<{/section}>


?

8、Smarty中使用類似for迴圈

用一段section類比
{section name=loop loop=$count}
id: {$smarty.section.loop.index}
{/section}


給count賦個值
$smarty->assign('count', 5);

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.