• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-23) 16次浏览 已收录 0个评论
文章目录[隐藏]
<?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(){//返回最后一条错误信息			ret<a style="color:transparent">来@源gao*daima.com搞@代#码网</a><strong>搞gaodaima代码</strong>urn $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`)

回复讨论(解决方案)

用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…… 唉- -总是些奇奇怪怪的小错误。。。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:求问我自己写的这个DB类,哪里错了,插不进数据
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址