首页  >  文章  >  php教程  >  php pdo连接数据库

php pdo连接数据库

PHP中文网
PHP中文网原创
2016-05-23 08:39:111126浏览

跳至

dbh = new PDO($dsn, $user, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
		} catch (PDOException $e) {
			echo 'Connection failed: ' . $e->getMessage();
		}
	}
	public function sRow($sql){
		$sth = $this->dbh->prepare($sql);
		$sth->execute();
		$ret = $sth->fetchAll(PDO::FETCH_ASSOC);
		return $ret[0];
	}
	public function sRows($sql){
		$sth = $this->dbh->prepare($sql);
		$sth->execute();
		return $sth->fetchAll(PDO::FETCH_ASSOC);
	}
	public function dbInsert($sql){
		$sth = $this->dbh;
		$count = $sth->exec($sql);
		if($count){
			return true;
		}else {
			return false;
		}
	}
	//insert update
	public function dbIUD($sql){
		$sth = $this->dbh;
		$count = $sth->exec($sql);
		if($count){
			return true;
		}else {
			return false;
		}
	}
}

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn