Heim >Backend-Entwicklung >PHP-Tutorial >php+mysql 面向对象 增删改查,该如何解决

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

WBOY
WBOYOriginal
2016-06-13 12:11:441387Durchsuche

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 />
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn