php的class类中使用mysqli的问题 $this->conn->query 这样写好像不对,应该怎么写,怎么改? 10
<?phpclassMySQLHelper{public$conn;public$host="localhost";public$password="root";publ...
<?php
class MySQLHelper{
public $conn;
public $host="localhost";
public $password="root";
public $user="root";
public $dbname="bbsservice";
public function __construct(){
$this->conn=new mysqli($this->host,$this->user,$this->password,$this->dbname);
if (!$this->conn){
die("connect erro".$conn->connect_error);
}
}
public function register($user_id,$user_name,$user_pw,$user_sex,$user_data){
echo "调用成功";
$this->conn->query("insert into userlist (user_id,user_name,user_pw,user_sex,user_data) values('$user_id','$user_name','$user_pw','$user_sex','$user_data');")
or die("insert erro".$this->conn->error);
echo succful;
$this->conn->close();
}
} 展开
class MySQLHelper{
public $conn;
public $host="localhost";
public $password="root";
public $user="root";
public $dbname="bbsservice";
public function __construct(){
$this->conn=new mysqli($this->host,$this->user,$this->password,$this->dbname);
if (!$this->conn){
die("connect erro".$conn->connect_error);
}
}
public function register($user_id,$user_name,$user_pw,$user_sex,$user_data){
echo "调用成功";
$this->conn->query("insert into userlist (user_id,user_name,user_pw,user_sex,user_data) values('$user_id','$user_name','$user_pw','$user_sex','$user_data');")
or die("insert erro".$this->conn->error);
echo succful;
$this->conn->close();
}
} 展开
2个回答
展开全部
那样写应该没错
你也可以这样写
class foo_mysqli extends mysqli {
public function __construct($host, $user, $pass, $db) {
parent::__construct($host, $user, $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
}
展开全部
class sqlhelper
{
private $mysqli;
private static $host='127.0.0.1';
private static $user='root';
private static $pwd='root';
private static $db='school';
public function __construct()
{
//初始化任务
$this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$db);
if($this->mysqli->connect_error)
{
exit('连接数据库失败'.$this->mysqli->connect_error);
}
//设置访问数据库字符集
$this->mysqli->query("set names utf8");
}
/*
* 数据查询语言DQL
/*数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE
*
* */
public function execute_dql($sql)
{
$result= $this->mysqli->query($sql) or exit('数据库查询sql失败'.$this->mysqli->error);
return $result;
}
/*
* 数据操纵语言DML
* 1) 插入:INSERT
*2) 更新:UPDATE
*3) 删除:DELETE
**/
public function execute_dml()
{ //影响行数
$row= $this->mysqli->query($sql) or exit('操作dml失败'.$this->mysqli->error);
if(!$row)
{
return 0;//表示失败
}
else
{
if($this->mysqli->affected_rows($row)>0)
{
return 1;//表示成功
}
else
{
return 2;//表示没有行受到影响
}
}
}
}
{
private $mysqli;
private static $host='127.0.0.1';
private static $user='root';
private static $pwd='root';
private static $db='school';
public function __construct()
{
//初始化任务
$this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$db);
if($this->mysqli->connect_error)
{
exit('连接数据库失败'.$this->mysqli->connect_error);
}
//设置访问数据库字符集
$this->mysqli->query("set names utf8");
}
/*
* 数据查询语言DQL
/*数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE
*
* */
public function execute_dql($sql)
{
$result= $this->mysqli->query($sql) or exit('数据库查询sql失败'.$this->mysqli->error);
return $result;
}
/*
* 数据操纵语言DML
* 1) 插入:INSERT
*2) 更新:UPDATE
*3) 删除:DELETE
**/
public function execute_dml()
{ //影响行数
$row= $this->mysqli->query($sql) or exit('操作dml失败'.$this->mysqli->error);
if(!$row)
{
return 0;//表示失败
}
else
{
if($this->mysqli->affected_rows($row)>0)
{
return 1;//表示成功
}
else
{
return 2;//表示没有行受到影响
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询