Home  >  Article  >  Backend Development  >  php+mysql 面向对象 增删改查,该如何解决

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

WBOY
WBOYOriginal
2016-06-13 12:11:441331browse

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 />
Statement:
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