首頁  >  文章  >  後端開發  >  php+mysql 面向对象 增删改查,该如何解决

php+mysql 面向对象 增删改查,该如何解决

WBOY
WBOY原創
2016-06-13 12:11:441358瀏覽

php+mysql 面向对象 增删改查
新手一枚,求指导。

<?php<br />//   Mysql_class.php<br />class Mysql{<br />    private $localhost;<br />    private $root;<br />    private $password;<br />	public  $database;<br /> <br />    public function __construct($localhost,$root,$password,$database){			//让下面的方式中,若要用到$localhost 变量<br />		$this->localhost = $localhost;											//就用$this->localhost 代替。 <br />		$this->root 	 = $root;<br />		$this->password  = $password;<br />		$this->database  = $database;<br />    }<br />	<br />	public function Connect(){<br />		mysql_connect	($this->localhost,$this-root,$this->password);<br />		mysql_select_db ($this->database);<br />		mysql_query 	("set names utf8");<br />	}<br />	<br />	public function Close(){<br />		mysql_close();<br />	}<br />	<br />	<br />	public function myarray($result){					//形参<br />		return mysql_fetch_array($result);<br />	}<br />	<br />	public function myquery($sql){<br />		return @mysql_query($sql);<br />	}<br />	<br />	public function myrows($result){<br />		return mysql_num_rows($result);<br />	}<br />	<br />	<br />	public function myselect($users){<br />		return $this->myquery("select * from $users");<br />	}<br />}<br /><br /><br />$db = new Mysql("localhost","root","","stu_system");


<center><br /><table cellpadding="18"><br />	<tr><br />		<th>id</th><br />		<th>name</th><br />		<th>sex</th><br />		<th>phone</th><br />	</tr><br /><?php<br />    include_once "mysql_class.php";<br />	<br />    $result = $db->myselect("users");<br /><br />	if(is_array($result)){<br />	while($row=$db->myarray($result)){<br />?>	<br />	<tr><br />		<td width="8%" align="center"> <?php echo $row['id'] 	 ?></td><br />		<td width="18%" align="center"><?php echo $row['name'] 	 ?></td><br />		<td width="8%" align="center"> <?php echo $row['sex'] 	 ?></td><br />		<td width="18%" align="center"><?php echo $row['phone']  ?></td><br />		<td><br />			<a href="modify.php?	id   = <?php echo $row['id']  ?>&<br />									name = <?php echo $row['name']?>&<br />									sex  = <?php echo $row['sex'] ?>&<br />									phone= <?php echo $row['phone'] ?>">  修改</a>         <!--   ?什么意思  为GET传输方式--><br />			<a href="delete.php?	id   = <?php echo $row['id']  ?>">	  删除</a><br />		</td><br />	</tr><br /></table><br /></center><br /><?php<br />	}<br />	}<br />	else echo"no result";<br />	mysql_close();<br />?>



获取不到我的资源,找不到问题在哪。
------解决思路----------------------
没有执行 Connect 方法
没有执行 myquery 方法

myselect 方法返回的是资源
------解决思路----------------------

引用:
没有执行 Connect 方法
没有执行 myquery 方法

myselect 方法返回的是资源

同上

<br /><?php<br />class Mysql{<br />    private $localhost;<br />    private $root;<br />    private $password;<br />    public  $database;<br />    private $link;<br />    private $res;<br />  <br />    public function __construct($localhost,$root,$password,$database){            //让下面的方式中,若要用到$localhost 变量<br />        $this->localhost = $localhost;                                            //就用$this->localhost 代替。 <br />        $this->root      = $root;<br />        $this->password  = $password;<br />        $this->database  = $database;        <br />    }<br />     <br />    public function Connect(){<br />        $this->link = mysql_connect($this->localhost, $this->root, $this->password);<br />        mysql_select_db ($this->database, $this->link );<br />        mysql_query     ("set names utf8");<br />    }<br />     <br />    public function Close(){<br />        mysql_close();<br />    }<br />     <br />     <br />    public function myarray($result){                    //形参<br />        return mysql_fetch_array($result);<br />    }<br />     <br />    public function myquery($sql){<br />        $this->res = mysql_query($sql) or die (mysql_error());<br />        return $this->res;<br />    }<br />     <br />    public function myrows($result){<br />        return mysql_num_rows($result);<br />    }<br />     <br />     <br />    public function myselect($users){<br />        return $this->myquery("select * from $users");<br />    }<br />}<br /> <br />$db?=?new?Mysql("localhost","root","","stu_system");<br />$db->Connect();<br /><br /><br /><center><br /><table cellpadding="18"><br />    <tr><br />        <th>id</th><br />        <th>name</th><br />        <th>sex</th><br />        <th>phone</th><br />    </tr><br /><?php<br />    include_once "mysql_class.php";<br />    $result = $db->myselect("users");<br />    while($row=$db->myarray($result)){<br />?>   <br />    <tr><br />        <td width="8%" align="center"> <?php echo $row['id']      ?></td><br />        <td width="18%" align="center"><?php echo $row['name']      ?></td><br />        <td width="8%" align="center"> <?php echo $row['sex']      ?></td><br />        <td width="18%" align="center"><?php echo $row['phone']  ?></td><br />        <td><br />            <a href="modify.php?    id   = <?php echo $row['id']  ?>&<br />                                    name = <?php echo $row['name']?>&<br />                                    sex  = <?php echo $row['sex'] ?>&<br />                                    phone= <?php echo $row['phone'] ?>">  修改</a>         <!--   ?什么意思  为GET传输方式--><br />            <a href="delete.php?    id   = <?php echo $row['id']  ?>">      删除</a><br />        </td><br />    </tr><br /></table><br /></center><br /><?php<br />    }<br />    mysql_close();<br />
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn