首頁  >  文章  >  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