<?php
class db{ public $host;
public $root;
public $pass;
public $dbname;
function __construct($host,$root,$pass,$dbname){ //传值时不能使用$this->host 这类写法
$link=mysql_connect($host,$root,$pass);
if(!$link){
echo "进入数据库出错!";
exit;
}else
if(!mysql_select_db($dbname)){
echo "进入数据库出错!";
exit;
}
$this->query_count=0; //查询次数 } //查询
function query($table,$filds="*",$where="1=1"){
$rs=mysql_query("select".$filds."FROM ".$table." where ".$where);
if($rs==false){
echo "查询失败!请查看传值有没有错误";
exit;
return false;
}
$this->query_count++; //查询次数
return $rs;
}
//插入
function inser($table,$filds,$value){ echo "INSERT INTO ".$table."(".$filds.")values(".$value.")";
/*
$arr=mysql_query("INSERT INTO ".$table."(".$filds.")values(".$value.")");
if($arr==false){
echo "插入失败!请查看传值有没有错误";
exit;
return false;
}
return true;
*/
}
//删除
function delete($table,$where){
$arr=mysql_query("delete from ".$table."where".$where);
if($arr==false){
echo "插入失败!请查看传值有没有错误";
exit;
return false;
}
return true;}
function update($table,$filds,$where){$arr=mysql_query("update ".$table." set ".$filds." where ".$where);
if($arr==false){
echo "插入失败!请查看传值有没有错误";
exit;
return false;
} return true;
}
function getrow($arr){ //得到行数
return mysql_num_rows($arr);
}function getaf(){ //得到影响行数
return mysql_affected_rows();
}
function free($arr){
@mysql_free_result($arr);
}function close($link='$link'){
mysql_close($link);
}}
?>源文件如下:
这样用
$rs=$db->query("lei","liename"," liename='$lei'");报错如下:
Fatal error: Call to a member function query() on a non-object in E:\AppServ\www\web\admin\paddlei.php on line 15完整如下:
<?php
/*
* Created on 2002-1-18
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include_once("../include/function.php");
createdb("../include/");//接库子
if(strlen(str_replace(" ","",$_POST["lei"]))<9){//至少要有3个汉字
echo "字数太少";
exit;
}
$lei=htmlspecialchars(substr($_POST["lei"],0,90));
$rs=$db->query("lei","liename"," liename='$lei'");
//$rs=@mysql_query("select liename from lei where liename='$lei'");
if(@mysql_num_rows($rs)){
echo "重名";
exit;
}
@mysql_query("set autocommit=0");
mysql_query("start transaction");
$f=@mysql_query("insert into lei(uplei,liename)values('$id','$lei')");
$id=mysql_insert_id();
$f1=@mysql_query("update lei set uplei='0,$id' where id='$id'");//$f1=$db->update("lei","uplei='$id'","where id='$id'");
if($f && $f1){
@mysql_query("commit");
@mysql_query("set autocommit=1");
goto("./addlei.php");
}else{
echo "error!";
@mysql_query("rollback");
@mysql_query("set autocommit=1");
exit;
}
$db->close();
$db->free($rs);
?>
class db{ public $host;
public $root;
public $pass;
public $dbname;
function __construct($host,$root,$pass,$dbname){ //传值时不能使用$this->host 这类写法
$link=mysql_connect($host,$root,$pass);
if(!$link){
echo "进入数据库出错!";
exit;
}else
if(!mysql_select_db($dbname)){
echo "进入数据库出错!";
exit;
}
$this->query_count=0; //查询次数 } //查询
function query($table,$filds="*",$where="1=1"){
$rs=mysql_query("select".$filds."FROM ".$table." where ".$where);
if($rs==false){
echo "查询失败!请查看传值有没有错误";
exit;
return false;
}
$this->query_count++; //查询次数
return $rs;
}
//插入
function inser($table,$filds,$value){ echo "INSERT INTO ".$table."(".$filds.")values(".$value.")";
/*
$arr=mysql_query("INSERT INTO ".$table."(".$filds.")values(".$value.")");
if($arr==false){
echo "插入失败!请查看传值有没有错误";
exit;
return false;
}
return true;
*/
}
//删除
function delete($table,$where){
$arr=mysql_query("delete from ".$table."where".$where);
if($arr==false){
echo "插入失败!请查看传值有没有错误";
exit;
return false;
}
return true;}
function update($table,$filds,$where){$arr=mysql_query("update ".$table." set ".$filds." where ".$where);
if($arr==false){
echo "插入失败!请查看传值有没有错误";
exit;
return false;
} return true;
}
function getrow($arr){ //得到行数
return mysql_num_rows($arr);
}function getaf(){ //得到影响行数
return mysql_affected_rows();
}
function free($arr){
@mysql_free_result($arr);
}function close($link='$link'){
mysql_close($link);
}}
?>源文件如下:
这样用
$rs=$db->query("lei","liename"," liename='$lei'");报错如下:
Fatal error: Call to a member function query() on a non-object in E:\AppServ\www\web\admin\paddlei.php on line 15完整如下:
<?php
/*
* Created on 2002-1-18
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include_once("../include/function.php");
createdb("../include/");//接库子
if(strlen(str_replace(" ","",$_POST["lei"]))<9){//至少要有3个汉字
echo "字数太少";
exit;
}
$lei=htmlspecialchars(substr($_POST["lei"],0,90));
$rs=$db->query("lei","liename"," liename='$lei'");
//$rs=@mysql_query("select liename from lei where liename='$lei'");
if(@mysql_num_rows($rs)){
echo "重名";
exit;
}
@mysql_query("set autocommit=0");
mysql_query("start transaction");
$f=@mysql_query("insert into lei(uplei,liename)values('$id','$lei')");
$id=mysql_insert_id();
$f1=@mysql_query("update lei set uplei='0,$id' where id='$id'");//$f1=$db->update("lei","uplei='$id'","where id='$id'");
if($f && $f1){
@mysql_query("commit");
@mysql_query("set autocommit=1");
goto("./addlei.php");
}else{
echo "error!";
@mysql_query("rollback");
@mysql_query("set autocommit=1");
exit;
}
$db->close();
$db->free($rs);
?>
解决方案 »
- 上海公司,年薪4万起+金,找php+mysql程序员(代发)
- 怎用curl抓取百度的mp3连接的地址
- 如何判断一个字符串里是否有中文或其他的ASIIC的可打印字符
- 求助:PHP+MYSQL网页显示乱码,高手帮帮我!搞的我实在无语了!
- 如何关闭窗口
- ASP代码改写成PHP代码-挑战高手!!!!!
- 我会ASP+SQLServer,PHP+MySQL好学吗?请大侠们给点意见。
- 我是jsp程序员,现在老板吩咐下来,1个月内要掌握php,并能进行维护,咋办,买什么书好啊
- 大甩分了,不够再加~
- php5.5 session_set_save_handler 连接数据库问题
- 菜鸟问个很简单的问题,马上给分!
- 怒了!!PHP到底怎样插入记录到数据库???
mysql_qurrey("select * ...", $link);//第二个参数$link不选的话,函数默认为最近链接数据库
//问题它是在不同成员函数操作,所以我想应该记录下来;
没见过这种查询。$db->query("SELECT * XXXXXXXXXXXXXXXXXXXXXXXXXX");
没有实例化具有 query() 方法的对象,对您而言,就是 $db 不存在或不是对象