Home > php教程 > PHP源码 > body text

php pdo连接数据库

PHP中文网
Release: 2016-05-23 08:39:11
Original
1130 people have browsed it

跳至

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;
		}
	}
}
Copy after login

                   

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template