我是这样设计的:一个是数据库连接文件 db.conn.php 一个自定义函数文件 config.inc.php
db.conn.php:
<?php$db_host = 'localhost';$db_user = '*****';$db_password = '*******';$db_database = '******';$conn = new mysqli($db_host, $db_user, $db_password, $db_database);mysqli_query($conn, "set names 'utf8'"); if(mysqli_connect_errno()){die('数据库连接失败: ' . mysqli_connect_error());}$site_data = $conn->query("SELECT * FROM `dh_member_control` WHERE `id`=1");$site = mysqli_fetch_array($site_data);?>
function marrbase(){if(isset($_COOKIE["uname"]) && isset($_COOKIE["uid"])){global $mbase;global $conn;$mql = "select * from dh_member where `id`=" . $_COOKIE["uid"] . " and `mck`=1";$mbase_data = mysqli_query($conn, $mql);$mbase = mysqli_fetch_array($mbase_data);}else{exit();}}function login_log($muid){if (empty($muid)){exit();}else {global $conn;$lql = "INSERT INTO `dh_mllog` (`uid`,`ltime`,`lip`,`lptxt`) VALUES (". $muid .",'". date("Y-m-d H:i:s") ."','". getIPaddress() ."','". getIPS138(getIPaddress()) ."')" ;if (!mysqli_query($conn, $lql)){die(mysqli_error());exit();}}}function memberislogin($back = NULL){if(!isset($_COOKIE["uname"]) || !isset($_COOKIE["uid"])){echo "<script>alert('没有登录');window.location.href=\"/member/login.php?backurl=". $back ."\";</script>"; exit();}else {global $conn;$login_check = mysqli_query ("SELECT * FROM `dh_member` WHERE `id`=". $_COOKIE["uid"] ." AND `mck`=1");if(!mysqli_num_rows($login_check)){echo "<script>window.location.href=\"/member/login.php?backurl=". $back ."\"</script>"; exit();}}}.....等等自定义函数
从 mysqli 继承一个 db 类
提供:
count 属性,用于返回影响的条数
fetch 方法,传入查询指令,返回一维数组
fetchall 方法,传入查询指令,返回二维数组
exec 方法,用于执行非查询类指令
基本就够了
学会封装代码,将mysql模块封装,其它需要用的地方,引用这个文件,直接调用封装的接口。
从 mysqli 继承一个 db 类
提供:
count 属性,用于返回影响的条数
fetch 方法,传入查询指令,返回一维数组
fetchall 方法,传入查询指令,返回二维数组
exec 方法,用于执行非查询类指令
基本就够了
学会封装代码,将mysql模块封装,其它需要用的地方,引用这个文件,直接调用封装的接口。
你总是有查询出多条记录的情况吧?比如文章列表
最简单的写法
class db extends mysqli { function __construct() { parent::__construct('localhost', '用户名', '口令', '数据库'); $this->query('set names utf8'); } function fetch($sql) { $rs = $this->query($sql); return $rs->fetch_assoc(); } function fetchall($sql) { $rs = $this->query($sql); while($row = $rs->fetch_assoc()) { $res[] = $row; } return $res; } funetion exec($sql) { return $this->query($sql); }}
$p = new db;print_r($p->fetch("select * from a"));print_r($p->fetchall("select * from a"));
你的 INSERT INTO `dh_mllog`.... 不就是非查询类的指令吗
你的 INSERT INTO `dh_mllog`.... 不就是非查询类的指令吗
你总是有查询出多条记录的情况吧?比如文章列表
最简单的写法
class db extends mysqli { function __construct() { parent::__construct('localhost', '用户名', '口令', '数据库'); $this->query('set names utf8'); } function fetch($sql) { $rs = $this->query($sql); return $rs->fetch_assoc(); } function fetchall($sql) { $rs = $this->query($sql); while($row = $rs->fetch_assoc()) { $res[] = $row; } return $res; } funetion exec($sql) { return $this->query($sql); }}
$p = new db;print_r($p->fetch("select * from a"));print_r($p->fetchall("select * from a"));
你的 INSERT INTO `dh_mllog`.... 不就是非查询类的指令吗
你总是有查询出多条记录的情况吧?比如文章列表
最简单的写法
class db extends mysqli { function __construct() { parent::__construct('localhost', '用户名', '口令', '数据库'); $this->query('set names utf8'); } function fetch($sql) { $rs = $this->query($sql); return $rs->fetch_assoc(); } function fetchall($sql) { $rs = $this->query($sql); while($row = $rs->fetch_assoc()) { $res[] = $row; } return $res; } funetion exec($sql) { return $this->query($sql); }}
$p = new db;print_r($p->fetch("select * from a"));print_r($p->fetchall("select * from a"));
<?$hot_vip_data = $conn->query("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`<>'/images/nan.png' AND `photo`<>'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12");$i=1;while ($hot_vip = mysqli_fetch_array($hot_vip_data)){if ($i % 6 == 0){$last=" class=\"last\"";}else{$last="";}$i++;?><li <? echo $last;?> onclick="javascript:location.href='/display/?<? echo $hot_vip["id"]?>.html'"><div class="vpc"><img src="<? echo $hot_vip["photo"]? alt="小白求指点:这样的代码结构怎么样优化呢?" >" /></div><div class="vpn"><? if (strlen($hot_vip["unick"])>4){echo cut_str($hot_vip["unick"],4);}else{echo $hot_vip["unick"];}if ($hot_vip["mpower"]==2){ echo " <a href=\"/vip/\" title=\"初遇会员\"><img src=\"/images/v.png\" / alt="小白求指点:这样的代码结构怎么样优化呢?" ></a>";} ?></div><div class="vpt"><? $timediff = strtotime (date("y-m-d")) - strtotime($hot_vip["sri"]) ;echo intval($timediff/(86400*365)) . "岁 ";echo $hot_vip["add"];?></div></li><? }?>
这样改写么?
<?$hvip = new db;$hvip_data = $hvip->fetchall("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`<>'/images/nan.png' AND `photo`<>'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12");$i=1;while ($hvip_data){if ($i % 6 == 0){$last = " class=\"last\"";}else{$last = "";}$i++;?><li <? echo $last;?> onclick="javascript:location.href='/display/?<? echo $hvip_data["id"]?>.html'"><div class="vpc"><img src="<? echo $hvip_data["photo"]? alt="小白求指点:这样的代码结构怎么样优化呢?" >" /></div><div class="vpn"><? if (strlen($hvip_data["unick"])>4){echo cut_str($hvip_data["unick"],4);}else{echo $hvip_data["unick"];}if ($hvip_data["mpower"]==2){ echo " <a href=\"/vip/\" title=\"初遇会员\"><img src=\"/images/v.png\" / alt="小白求指点:这样的代码结构怎么样优化呢?" ></a>";} ?></div><div class="vpt"><? $timediff = strtotime (date("y-m-d")) - strtotime($hvip_data["sri"]) ;echo intval($timediff/(86400*365)) . "岁 ";echo $hvip_data["add"];?></div></li><? }?>
<?$hvip = new db;$sql = $hvip->fetchall("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`<>'/images/nan.png' AND `photo`<>'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12";foreach($hvip->fetchall($sql) as $i=>$hvip_data) { if (($i+1) % 6 == 0) $last = " class=\"last\""; else $last = "";?><li <? echo $last;?> onclick="javascript:location.href='/display/?<? echo $hvip_data["id"]?>.html'"><div class="vpc"><img src="<? echo $hvip_data["photo"]? alt="小白求指点:这样的代码结构怎么样优化呢?" >" /></div><div class="vpn"><? if (strlen($hvip_data["unick"])>4){echo cut_str($hvip_data["unick"],4);}else{echo $hvip_data["unick"];}if ($hvip_data["mpower"]==2){ echo " <a href=\"/vip/\" title=\"初遇会员\"><img src=\"/images/v.png\" / alt="小白求指点:这样的代码结构怎么样优化呢?" ></a>";} ?></div><div class="vpt"><? $timediff = strtotime (date("y-m-d")) - strtotime($hvip_data["sri"]) ;echo intval($timediff/(86400*365)) . "岁 ";echo $hvip_data["add"];?></div></li><? }?>
<?$hvip = new db;$sql = $hvip->fetchall("SELECT * FROM dh_member WHERE `mck`=1 AND `photo`<>'/images/nan.png' AND `photo`<>'/images/nv.png' ORDER BY `mpower` DESC,`cktime` DESC LIMIT 1,12";foreach($hvip->fetchall($sql) as $i=>$hvip_data) { if (($i+1) % 6 == 0) $last = " class=\"last\""; else $last = "";?><li <? echo $last;?> onclick="javascript:location.href='/display/?<? echo $hvip_data["id"]?>.html'"><div class="vpc"><img src="<? echo $hvip_data["photo"]? alt="小白求指点:这样的代码结构怎么样优化呢?" >" /></div><div class="vpn"><? if (strlen($hvip_data["unick"])>4){echo cut_str($hvip_data["unick"],4);}else{echo $hvip_data["unick"];}if ($hvip_data["mpower"]==2){ echo " <a href=\"/vip/\" title=\"初遇会员\"><img src=\"/images/v.png\" / alt="小白求指点:这样的代码结构怎么样优化呢?" ></a>";} ?></div><div class="vpt"><? $timediff = strtotime (date("y-m-d")) - strtotime($hvip_data["sri"]) ;echo intval($timediff/(86400*365)) . "岁 ";echo $hvip_data["add"];?></div></li><? }?>