php mysql_query() 方法的问题 10
<?phpclassMysql{private$host;private$user;private$pwd;private$dbName;private$charset;...
<?php
class Mysql{
private $host;
private $user;
private $pwd;
private $dbName;
private $charset;
private $conn = null;
public function __construct(){
$this -> host = 'localhost';
$this -> user = 'root';
$this -> pwd = '';
$this -> dbName = 'test';
$this -> connect($this -> host, $this -> user, $this -> pwd);
$this -> switchDb($this -> dbName);
$this -> setChar($this -> charset);
}
private function connect($h, $u, $p){
$conn = mysqli_connect($h, $u, $p);
$this -> conn = $conn;
}
public function switchDb($db){
$sql = 'use '. $db;
$this -> query($sql);
}
public function setChar($char){
$sql = 'set names ' . $char;
$this -> query($sql);
// mysql_query($sql);
}
public function query($sql){
return mysql_query($this -> conn, $sql) ;
}
public function getAll($sql){
}
}
$mysql = new Mysql();
$sql = "insert into user values (6, 'mc', 99)";
if($mysql -> query($sql)){
echo 'query ok';
}else{
echo 'fail';
}
?>
输出的结果是
Warning: mysql_query() expects parameter 1 to be string, object given in
fail
不知道是怎么回事,请高手指教. 展开
class Mysql{
private $host;
private $user;
private $pwd;
private $dbName;
private $charset;
private $conn = null;
public function __construct(){
$this -> host = 'localhost';
$this -> user = 'root';
$this -> pwd = '';
$this -> dbName = 'test';
$this -> connect($this -> host, $this -> user, $this -> pwd);
$this -> switchDb($this -> dbName);
$this -> setChar($this -> charset);
}
private function connect($h, $u, $p){
$conn = mysqli_connect($h, $u, $p);
$this -> conn = $conn;
}
public function switchDb($db){
$sql = 'use '. $db;
$this -> query($sql);
}
public function setChar($char){
$sql = 'set names ' . $char;
$this -> query($sql);
// mysql_query($sql);
}
public function query($sql){
return mysql_query($this -> conn, $sql) ;
}
public function getAll($sql){
}
}
$mysql = new Mysql();
$sql = "insert into user values (6, 'mc', 99)";
if($mysql -> query($sql)){
echo 'query ok';
}else{
echo 'fail';
}
?>
输出的结果是
Warning: mysql_query() expects parameter 1 to be string, object given in
fail
不知道是怎么回事,请高手指教. 展开
2个回答
展开全部
/*把这个方法的参数写错了*/
public function query($sql){
return mysql_query($this -> conn, $sql) ;
}
/*应该修改为*/
public function query($sql){
return mysql_query($sql,$this -> conn) ;
}
更多追问追答
追问
用mysqli时 不是就要 反过来写吗?
如果用($sql,$this -> conn)就会输出:
Warning: mysql_query() expects parameter 2 to be resource, object given
追答
/*把这个方法修改一下*/
private function connect($h, $u, $p){
$conn = mysqli_connect($h, $u, $p);
$this -> conn = $conn;
}
/*修改为如下,其中$dbname是你要连接的数据库名*/
private function connect($h, $u, $p,$dbname){
$conn = new mysqli_connect($h, $u, $p,$dbname);
$this -> conn = $conn;
}
2015-05-18
展开全部
你到底是msyql还是mysqli?
仔细检查下!
仔细检查下!
追问
是mysqli
追答
<?php
class Mysql{
private $host, $user, $pwd, $dbName, $charset;
private $conn = object;
public function __construct($host = 'localhost', $user = 'root', $pwd = '', $dbName = 'test', $charset = 'utf8'){
$this -> host = $host;
$this -> user = $user;
$this -> pwd = $pwd;
$this -> dbName = $dbName;
$this -> charset = $charset;
$this -> connect();
}
private function connect(){
$this -> conn = @new mysqli( $this->host, $this->user, $this->pwd, $this->dbName);
!mysqli_connect_errno() or die('数据库连接失败(' . mysqli_connect_errno() . '):' . mysqli_connect_error() );
$this->conn->query('set names ' . $this->charset);
}
public function query($sql){
return $this->conn->query( $sql );
}
}
$mysql = new Mysql;
$sql = "insert into user values (6, 'mc', 99)";
echo $mysql->query( $sql ) ? 'query ok' : 'fail';
你没有理解到类的结构好作用!
既然使用类, 那最基本的是有类成员和类方法!
类成员和方法之间可以相互调用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询