Home > Backend Development > PHP Tutorial > 小白求指点:这样的代码结构怎么样优化呢?

小白求指点:这样的代码结构怎么样优化呢?

WBOY
Release: 2016-06-23 13:34:10
Original
838 people have browsed it

我是这样设计的:一个是数据库连接文件 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);?>
Copy after login


config.inc.php:

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();}}}.....等等自定义函数
Copy after login


在用户前台 引用进这些文件 然后进行输出交互等操作..

这里面我目前就觉着 我每次自定义个函数 都要 global $conn 来引用,有没有更好的办法来设计?
整个网页设计有没有更好的设计方式呢(CMS就不要推荐了)
求做过大项目的大神指点指点..


回复讨论(解决方案)

从 mysqli 继承一个 db 类
提供:
count 属性,用于返回影响的条数
fetch 方法,传入查询指令,返回一维数组
fetchall 方法,传入查询指令,返回二维数组
exec 方法,用于执行非查询类指令
基本就够了

学会封装代码,将mysql模块封装,其它需要用的地方,引用这个文件,直接调用封装的接口。

从 mysqli 继承一个 db 类
提供:
count 属性,用于返回影响的条数
fetch 方法,传入查询指令,返回一维数组
fetchall 方法,传入查询指令,返回二维数组
exec 方法,用于执行非查询类指令
基本就够了



谢谢两位大神啊 我之前看过有这样的资源 就是常用数据库操作类 是这样类似的用途么

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);  }}
Copy after login
Copy after login
Copy after login

$p = new db;print_r($p->fetch("select * from a"));print_r($p->fetchall("select * from a"));
Copy after login
Copy after login
Copy after login


由于是继承 mysqli 类的,所以 mysqli 类的方法、属性都存在,不必一一抄写

你的 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);  }}
Copy after login
Copy after login
Copy after login

$p = new db;print_r($p->fetch("select * from a"));print_r($p->fetchall("select * from a"));
Copy after login
Copy after login
Copy after login


由于是继承 mysqli 类的,所以 mysqli 类的方法、属性都存在,不必一一抄写



谢谢版主


你的 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);  }}
Copy after login
Copy after login
Copy after login

$p = new db;print_r($p->fetch("select * from a"));print_r($p->fetchall("select * from a"));
Copy after login
Copy after login
Copy after login


由于是继承 mysqli 类的,所以 mysqli 类的方法、属性都存在,不必一一抄写



谢谢版主



弱弱的问下啊,下面这段,按你这样的类方法调用该怎么改写啊?

<?$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><? }?>
Copy after login


这样改写么?

<?$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><? }?>
Copy after login

<?$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><? }?>
Copy after login
Copy after login

<?$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><? }?>
Copy after login
Copy after login



谢谢版主
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template