求问我自己写的这个DB类,哪里错了,插不进数据

php中文网
发布: 2016-06-23 14:05:36
原创
1470人浏览过

<?php	class DB{		private $hostname;		private $username;		private $password;		private $select_db;		private $con;		private $Error;				public function __construct($hostname,$username,$password,$select_db){			if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值			{				$this->hostname = $hostname;				$this->username = $username;				$this->password = $password;				$this->select_db = $select_db;				$this->con = mysql_connect($this->hostname,$this->username,$this->password);				if(!$this->con){					$this->Error = die('Could Not Connect:'.mysql_error);				}				else{					mysql_select_db($this->select_db,$this->con);				}			}		}		public function __destruct(){//退出时结束连接			mysql_close($this->con);		}		public function insert($table,$body){//插入table中的一个数组			$line1 = implode(',',$body);			$line2 = implode(',',array_keys($body));			echo $sql = "INSERT INTO $table ($line2) VALUES ($line1)";			$result = mysql_query($sql,$this->con);			if(!$result){				echo $this->Error;				echo '111';			}					}		public function update($table,$body){				}		public function read($table,$keyword){					}		public function delete($table,$keyword){				}		public function getLastError(){//返回最后一条错误信息			return $this->Error;		}	}?>
登录后复制

主页调用的是
<?phprequire('DB.class.php');	$DB = new DB('localhost','root','','dbtest');	$line1 = array(			'aa'=>'`aa`',			'bb'=>'`dd`'	);	$DB->insert('1234',$line1);?>
登录后复制

echo $sql语句是INSERT INTO 1234 (aa,bb) VALUES (`aa`,`dd`)

阿里云-虚拟数字人
阿里云-虚拟数字人

阿里云-虚拟数字人是什么? ...

阿里云-虚拟数字人 2
查看详情 阿里云-虚拟数字人

回复讨论(解决方案)

用mysql_error查询$this->con,没有返回错误信息。。。。我现在很抓狂- -求人解答啊。。。。

require('DB.class.php');
    $DB = new DB('localhost','root','','dbtest');
    $line1 = array(
            'aa'=> "'aa'",
            'bb'=> "'dd'"
    );
    $DB->insert('1234',$line1);

你的$line1数组写反了。
而且insert 语句还有一种格式:insert into tb_member set username = "test", type = 1, lastlogindt = now()。跟update样式差不多。
你也可以看看人家写的数据库类,我感觉挺好的:http://www.cnblogs.com/hooray/archive/2012/07/21/2603017.html

你的$line1数组写反了。
而且insert 语句还有一种格式:insert into tb_member set username = "test", type = 1, lastlogindt = now()。跟update样式差不多。
你也可以看看人家写的数据库类,我感觉挺好的:http://www.cnblogs.com/hooray/archive/201…… 其实没写反- -三楼指出来了。。。都是些小符号的问题。。。。。

require('DB.class.php');
    $DB = new DB('localhost','root','','dbtest');
    $line1 = array(
            'aa'=> "'aa'",
            'bb'=> "'dd'"
    );
    $DB->insert('1234',$li…… 唉- -总是些奇奇怪怪的小错误。。。

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号