php连接mysql成功了,但是插入数据失败! 5
<?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=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){
}
}
//测试是否连接成功 Resource id #3
$mysql=new Mysql();
print_r($mysql);
$sql="insert into user values ( 5, '小明', 23 )";
if($mysql->query($sql)){
echo'插入成功!';
}else{
echo'插入失败!';
}
//代码结束 运行结果为 插入失败!
?> 展开
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=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){
}
}
//测试是否连接成功 Resource id #3
$mysql=new Mysql();
print_r($mysql);
$sql="insert into user values ( 5, '小明', 23 )";
if($mysql->query($sql)){
echo'插入成功!';
}else{
echo'插入失败!';
}
//代码结束 运行结果为 插入失败!
?> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询