当前位置:Gxlcms > PHP教程 > php+mysql面向对象增删改查,该如何解决

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

时间:2021-07-01 10:21:17 帮助过:14人阅读

php+mysql 面向对象 增删改查
新手一枚,求指导。
//   Mysql_class.php
class Mysql{
private $localhost;
private $root;
private $password;
public $database;

public function __construct($localhost,$root,$password,$database){ //让下面的方式中,若要用到$localhost 变量
$this->localhost = $localhost; //就用$this->localhost 代替。
$this->root = $root;
$this->password = $password;
$this->database = $database;
}

public function Connect(){
mysql_connect ($this->localhost,$this-root,$this->password);
mysql_select_db ($this->database);
mysql_query ("set names utf8");
}

public function Close(){
mysql_close();
}


public function myarray($result){ //形参
return mysql_fetch_array($result);
}

public function myquery($sql){
return @mysql_query($sql);
}

public function myrows($result){
return mysql_num_rows($result);
}


public function myselect($users){
return $this->myquery("select * from $users");
}
}


$db = new Mysql("localhost","root","","stu_system");










include_once "mysql_class.php";

$result = $db->myselect("users");

if(is_array($result)){
while($row=$db->myarray($result)){
?>







id name sex phone

&
name = &
sex = &
phone= "> 修改
"> 删除


}
}
else echo"no result";
mysql_close();
?>



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

myselect 方法返回的是资源
------解决思路----------------------
引用:
没有执行 Connect 方法
没有执行 myquery 方法

myselect 方法返回的是资源

同上


class Mysql{
private $localhost;
private $root;
private $password;
public $database;
private $link;
private $res;

public function __construct($localhost,$root,$password,$database){ //让下面的方式中,若要用到$localhost 变量
$this->localhost = $localhost; //就用$this->localhost 代替。
$this->root = $root;
$this->password = $password;
$this->database = $database;
}

public function Connect(){
$this->link = mysql_connect($this->localhost, $this->root, $this->password);
mysql_select_db ($this->database, $this->link );
mysql_query ("set names utf8");
}

public function Close(){
mysql_close();
}


public function myarray($result){ //形参
return mysql_fetch_array($result);
}

public function myquery($sql){
$this->res = mysql_query($sql) or die (mysql_error());
return $this->res;
}

public function myrows($result){
return mysql_num_rows($result);
}


public function myselect($users){
return $this->myquery("select * from $users");
}
}

$db?=?new?Mysql("localhost","root","","stu_system");
$db->Connect();










include_once "mysql_class.php";
$result = $db->myselect("users");
while($row=$db->myarray($result)){
?>







id name sex phone

&
name = &
sex = &
phone= "> 修改
"> 删除


}
mysql_close();

人气教程排行