<?php
/*
* Created on 2012-1-21
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class mysql{
private $host;
private $name;
private $pass;
private $table;
private $ut;
function __construct($host,$name,$pass,$table,$ut){//这儿一定要先初始化table
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->table=$table;
$this->ut=$ut;
$this->connect(); }
function connect(){
$link=mysql_connect($this->host,$this->name,$this->pass) or die($this->error());
mysql_select_db($this->table,$link) or die("没有该数据库:".$this->table);//此处的link
mysql_query("set names '$this->ut'");//这个单双引号很关键
}
function query($v){
return mysql_query($v);//很有用的类
}
function error(){
return mysql_error();
}
//-----------------------
function fn_insert($table,$name,$value){
$this->query("insert into $table ($name) $value($value)");
}
}
$db=new mysql("localhost","root","","bbs","gbk");//此处要加编码
$db->fn_insert("message","id,user,title,content,lastdate","'','龙爷','我插入的信息','你好',now()");//now不用双引号,此处的单双引号很重要
?>
我的这段代码问什么数据插入不到数据库呢
还有,为什么"'','龙爷','我插入的信息','你好',now()"里面非要用单引号才不提示错误呢?
谢谢大家
/*
* Created on 2012-1-21
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class mysql{
private $host;
private $name;
private $pass;
private $table;
private $ut;
function __construct($host,$name,$pass,$table,$ut){//这儿一定要先初始化table
$this->host=$host;
$this->name=$name;
$this->pass=$pass;
$this->table=$table;
$this->ut=$ut;
$this->connect(); }
function connect(){
$link=mysql_connect($this->host,$this->name,$this->pass) or die($this->error());
mysql_select_db($this->table,$link) or die("没有该数据库:".$this->table);//此处的link
mysql_query("set names '$this->ut'");//这个单双引号很关键
}
function query($v){
return mysql_query($v);//很有用的类
}
function error(){
return mysql_error();
}
//-----------------------
function fn_insert($table,$name,$value){
$this->query("insert into $table ($name) $value($value)");
}
}
$db=new mysql("localhost","root","","bbs","gbk");//此处要加编码
$db->fn_insert("message","id,user,title,content,lastdate","'','龙爷','我插入的信息','你好',now()");//now不用双引号,此处的单双引号很重要
?>
我的这段代码问什么数据插入不到数据库呢
还有,为什么"'','龙爷','我插入的信息','你好',now()"里面非要用单引号才不提示错误呢?
谢谢大家
$this->query("insert into $table ($name) $value($value)");changeto$this->query("insert into $table ($name) VALUES ($value)");2.
在SQL中属于字符串类型的都需要使用引号括起来。int数值类型可以不需要。