php 封装MySQL类怎么,不能执行sql语句query()
<?PHPclassmysql{private$host;private$user;private$pwd;private$dbname;private$charset;...
<?PHP
class mysql{
private $host;
private $user;
private $pwd;
private $dbname;
private $charset;
public $conn = null;//保存连接的资源
public function __construct(){
//应该是在构造方法里,读取配置文件
$this->host ='localhost';
$this->user = 'root';
$this->pwd = 'root';
$this->dbname = 'crm';
//连接
$this->connect($this->host,$this->user,$this->pwd);
//切换库
$this->switchdb($this->dbname);
//设置字符集
$this->setchar($this->charset);
}
//负责连接
private function connect($h,$u,$p){
$conn = mysql_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);
}
//负责发送sql查询
public function query($sql){
return mysql_query($sql,$this->conn);
}
//负责获取多行多列的select结果
public function getall($sql){
}
}
$mysql = new mysql();
print_r($mysql);
$sql = "create table as";
var_dump($mysql->query($sql))
/* {
echo 'chenggong';
}else{
echo 'shib';
}
*/
?>
调试显示mysql Object ( [host:mysql:private] => localhost [user:mysql:private] => root [pwd:mysql:private] => root [dbname:mysql:private] => crm [charset:mysql:private] => [conn] => Resource id #3 ) bool(false),为什么连接数据库能连上,但是执行sql语句不行呢 展开
class mysql{
private $host;
private $user;
private $pwd;
private $dbname;
private $charset;
public $conn = null;//保存连接的资源
public function __construct(){
//应该是在构造方法里,读取配置文件
$this->host ='localhost';
$this->user = 'root';
$this->pwd = 'root';
$this->dbname = 'crm';
//连接
$this->connect($this->host,$this->user,$this->pwd);
//切换库
$this->switchdb($this->dbname);
//设置字符集
$this->setchar($this->charset);
}
//负责连接
private function connect($h,$u,$p){
$conn = mysql_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);
}
//负责发送sql查询
public function query($sql){
return mysql_query($sql,$this->conn);
}
//负责获取多行多列的select结果
public function getall($sql){
}
}
$mysql = new mysql();
print_r($mysql);
$sql = "create table as";
var_dump($mysql->query($sql))
/* {
echo 'chenggong';
}else{
echo 'shib';
}
*/
?>
调试显示mysql Object ( [host:mysql:private] => localhost [user:mysql:private] => root [pwd:mysql:private] => root [dbname:mysql:private] => crm [charset:mysql:private] => [conn] => Resource id #3 ) bool(false),为什么连接数据库能连上,但是执行sql语句不行呢 展开
1个回答
推荐于2016-11-28
展开全部
看不懂你写的什么。给个现成的你
<?php
/**
* Created by PhpStorm.
* User: TAOYU
* Date: 14-11-16
* Time: 上午1:28
*/
class mysql
{
protected $host;
protected $user;
protected $pwd;
protected $port;
protected $error;
protected $db;
protected $charset;
protected $conn = null;
public static $total;//获得总条数
public static $pages;//总页数
public static $pagesize;//每页显示条数
public $act_page;//获取当前页码
public $start;//开始条数
//构造方法,初始化时连接数据库
public function __construct($h = 'localhost', $u = 'root', $pwd = '123', $port = 3306)
{
$this->host = $h;
$this->user = $u;
$this->pwd = $pwd;
$this->port = $port;
$this->connect();
$this->selectDb('bookboss');
$this->setChar('utf8');
}
public function __destruct()
{
mysql_close();
}
//连接方法
public function connect()
{
if ($this->conn = mysql_connect($this->host, $this->user, $this->pwd, $this->port)) {
return true;
} else {
$this->error = "连接失败!";
return false;
}
}
//选库方法
public function selectDb($dbName)
{
//use后要有空格!!!注意!!!
$sql = "use " . $dbName;
$this->db = $dbName;
return $this->query($sql);
}
//设置字符集方法
public function setChar($char)
{
//set names后要有空格!!!注意!!!
$sql = "set names " . $char;
return $this->query($sql);
}
//查询方法
public function query($sql)
{
$rs = mysql_query($sql, $this->conn);
if (!$rs) {
/*$this->error=mysql_error($this->conn);
$this->log($this->error);*/
return false;
} else {
return $rs;
}
}
//取指定数据
/* public function getData($page,$pagesize=5)
{
$start=($page-1)*$pagesize;
$rs = $this->query("select * from student limit $start,$pagesize");
if (!$rs) {
return false;
} else {
$list = array();
while ($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
return $list;
}
}*/
//取数据
public function getAll($sql)
{
$rs = $this->query($sql);
if (!$rs) {
return false;
} else {
$list = array();
while ($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
return $list;
}
}
//返回sql语句结果条数
public function getNums($sql){
return mysql_num_rows($this->query($sql));
}
//insert插入数据方法
public function insert($sql)
{
}
//读取错误方法
public function getError()
{
return $this->error;
}
//记录日志方法
/* public function log($err){
$time=date('Y-m-d H:i:s',time());
if(file_exists("./log.txt")){
$contents=file_get_contents("./log.txt");
$contents.="$time\t".$err."\r\n";
file_put_contents('./log.txt',$contents);
}else{
$filename='./log.txt';
$str='';
writefile($filename,$str);
$contents=file_get_contents("./log.txt");
$contents.="$time\t".$err."\r\n";
file_put_contents('./log.txt',$contents);
}
}*/
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |