全部纯静态化 — 局部纯静态化
实现步骤:
1. 编写接口
<code><span><span><?php </span><span>//排行榜接口数据</span><span>//1. 获取数据</span><span>//2. 把我们获取到的数据组装成接口数据提供通信</span><span>//<strong>连接数据库</strong></span><span><strong>require</strong>_once</span>(<span>'./db.php'</span>); <span>$connect</span> = Db::getInstance()->connect(); <span>$sql</span> = <span>'select * from hit as a join news as b on a.news_id=b.id order by a.count desc limit 3'</span>; <span>$result</span> = mysql_query(<span>$sql</span>,<span>$connect</span>); <span>while</span>(<span>$row</span> = mysql_fetch_assoc(<span>$result</span>)) { <span>$res</span>[] = <span>$row</span>; } <span>// 1 代表成功代码</span><span>return</span> show(<span>1</span>,<span>'success'</span>,<span>$res</span>); <span>//将数据生成接口数据</span><span><span>function</span><span>show</span><span>(<span>$code</span>=<span>0</span>,<span>$message</span>=<span>'error'</span>,<span>$data</span>=array<span>()</span>)</span> {</span><span>$result</span> = <span>array</span>( <span>'code'</span>=><span>$code</span>, <span>'message'</span>=><span>$message</span>, <span>'data'</span>=><span>$data</span>, ); <span>echo</span> json_encode(<span>$result</span>); }</span></span></code>
<code>插件名称:<span>JSON</span>-handle</code>
以上就介绍了51 PHP 页面静态化4,包括了require,连接数据库方面的内容,希望对PHP教程有兴趣的朋友有所帮助。