php查询数据库id为6相应的内容,PHP中写一个数据库查询的类的方法代码要如何写...

PHP中写一个数据库查询的类的方法代码要如何写

mip版  关注:290  答案:2  悬赏:20

解决时间 2021-01-19 11:03

371bd4c659ed6e9de3f2999d507716ad.png

已解决

2021-01-18 22:49

我这有如许的类

最佳答案

bb1de14f1a788b1816c11af2c8ac419a.png

2021-01-19 00:11

不要为了类而写类,你要看你自己的应用需求。

找市面上有的php数据库类参考一下。简化一下。就可以写你自己的数据库查询类。

全部回答

95cb910a0afe4c2e33f8982cfc189d19.png

1楼

2021-01-19 01:43

host = $host;

$this->user = $user;

$this->pass = $pass;

$this->charset = $charset;

}

function connect ($db = "") {

global $cfg_mysql_info;

if (!$this->host) {

$this->host = $cfg_mysql_info["host"];

}

if (!$this->user) {

$this->user = $cfg_mysql_info["user"];

}

if (!$this->pass) {

$this->pass = $cfg_mysql_info["passwd"];

}

if (!$this->charset) {

$this->charset = "utf8";

}

if (empty($db))

$this->db = $cfg_mysql_info["database"];

else

$this->db = $db;

$this->id = @mysql_connect($this->host, $this->user, $this->pass);

if (!$this->id)

return false;

$this->selectdb($this->db);

$this->query("set names '".$this->charset."'");

return true;

}

function close(){

@mysql_close($this->id);

}

function selectdb ($db) {

if(!@mysql_select_db($db, $this->id))

return false;

else

return true;

}

function begin () {

$this->result = @mysql_query("start transaction with consistent snapshot", $this->id);

if (!$this->result)

return false;

return true;

}

function commit () {

$this->result = @mysql_query("commit", $this->id);

if (!$this->result)

return false;

return true;

}

function rollback () {

$this->result = @mysql_query("rollback", $this->id);

if (!$this->result)

return false;

return true;

}

function escape ($str) {

$escstr = mysql_escape_string($str);

return $escstr;

}

# 普通查询功能,主要用于返回结果是多条记录的情况

# 请使用 fetch 方法取得每条记录信息

function query ($query) {

$this->result = @mysql_query($query, $this->id);

if (!$this->result)

{

if ($this->debug)

mysql_errormsg ("不能执行查询(query): $query");

else

return false;

}

$this->rows = @mysql_num_rows($this->result);

$this->fields = @mysql_num_fields($this->result);

if (!$this->rows) return false;

return true;

}

function querysql ($query) {

$ret = @mysql_query($query, $this->id);

if ($ret === false)

{

if ($this->debug)

mysql_errormsg ("不能执行查询(query): $query");

else

return false;

}

$this->result = $ret;

$this->rows = @mysql_num_rows($this->result);

$this->fields = @mysql_num_fields($this->result);

return true;

}

# 如果查询结果为单条记录时使用,返回结果存储于数组 data 中

function queryrow ($query) {

$this->result = @mysql_query($query, $this->id);

if (!$this->result)

{

if ($this->debug)

mysql_errormsg ("不能执行查询(query): $query");

else

return false;

}

$this->rows = @mysql_num_rows($this->result);

$this->data = @mysql_fetch_array($this->result, mysql_assoc);

//mysql_errormsg ("不能从查询结果中取得数据 $query");

if (!$this->result || !$this->rows)

return false;

return true;

}

# 移动到指定记录行,将该行结果储存于数组 data 中

function fetch ($row) {

if(!@mysql_data_seek($this->result, $row))

//mysql_errormsg ("不能定位到指定数据行 $row");

return false;

$this->data = @mysql_fetch_array($this->result, mysql_assoc);

//mysql_errormsg ("不能提取指定数据行数据 $row");

if (!$this->data)

return false;

return true;

}

function insert ($query) {

$this->result = @mysql_query($query, $this->id);

if (!$this->result)

{

if ($this->debug)

mysql_errormsg ("不能执行查询(query): $query");

else

return false;

}

$this->arows = @mysql_affected_rows($this->id);

$this->iid = @mysql_insert_id($this->id);

return true;

}

function update ($query) {

$this->result = @mysql_query($query, $this->id);

if (!$this->result)

{

if ($this->debug)

mysql_errormsg ("不能执行查询(query): $query");

else

return false;

}

$this->arows = @mysql_affected_rows($this->id);

if (!$this->arows || $this->arows == -1)

return false;

return true;

}

function delete ($query) {

$this->result = @mysql_query($query, $this->id);

if (!$this->result)

{

if ($this->debug)

mysql_errormsg ("不能执行查询(query): $query");

else

return false;

}

$this->arows = @mysql_affected_rows($this->id);

return true;

}

function error (){

return mysql_error()."(".mysql_errno().")";

}

function errno (){

return mysql_errno();

}

}

function mysql_errormsg ($msg) {

# 关闭可能影响字符显示的html代码

echo("\n");

echo("\n");

# 错误信息

$text = "系统提示:".$msg."

";

$text .= "错误信息:";

$text .= mysql_error()."

";

$text .= "错误代码:".mysql_errno()."

";

$text .= "请稍候再试,如果问题仍然存在,请与 系统管理员 联系!";

$text .= "\n";

die($text);

}

}

?>

一些细节的地方自己修改吧 主要是我在别的文件专门定义了全局变量,你看一遍,把应改的地方改一下就好了

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

点此我要举报以上信息!

推荐资讯

大家都在看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值