class Base {
static $DB;
protected $sth;
static $stmt = array();
function __construct() {
// 读取配置文件
$dns = "mysql:host=localhost;dbname=test";
$psw = "123456";
$username = "root";
self::$DB = new PDO($dns, $username, $psw);
self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
function prepareStatement($stmt_s) {
$stmt_handle = self::$DB->prepare($stmt_s);
self::$stmt[$stmt_s] = $stmt_handle;
return $stmt_handle;
}
function doStatement($stmt_s, $value_s) {
$sth = $this->prepareStatement($stmt_s);
$sth->closeCursor();
$db_result = $sth->execute($value_s);
if ($db_result) {
echo "sql语句执行成功<br/>";
} else {
echo "sql语句执行失败<br/>";
}
$this->sth = $sth;
}
function fetchStatement() {
while($row = $this->sth->fetch(PDO::FETCH_ASSOC)){
foreach ($row as $key => $val) {
echo "$key -- $val ";
}
echo "<br/>";
}
}
}
$sql = "select * from price where id < ?;";
$sql2 = "insert into price (goodprice) values (?);";
$values = 21;
try{
$db = new Base();
$db->doStatement($sql, $values );
echo "";
$db->fetchStatement();
Base::$stmt[$sql]->closeCursor();
$db->doStatement($sql2, $values );
print_r(Base::$DB->lastInsertId());
echo "<br/>上次mysql操作影响" .Base::$stmt[$sql2]->rowCount() . "行"; //PDOStatement::rowCount
} catch(Exception $e) {
echo "错误" .$e->getMessage();
}
Warning: PDOStatement::execute() expects parameter 1 to be array, integer given in D:\apache2.4.10VC9\Apache24\htdocs\test\4.php on line 58
sql语句执行失败
Warning: PDOStatement::execute() expects parameter 1 to be array, integer given in D:\apache2.4.10VC9\Apache24\htdocs\test\4.php on line 58
sql语句执行失败
0
上次mysql操作影响0行
为什么php 抛出异常但继续往下执行
static $DB;
protected $sth;
static $stmt = array();
function __construct() {
// 读取配置文件
$dns = "mysql:host=localhost;dbname=test";
$psw = "123456";
$username = "root";
self::$DB = new PDO($dns, $username, $psw);
self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
function prepareStatement($stmt_s) {
$stmt_handle = self::$DB->prepare($stmt_s);
self::$stmt[$stmt_s] = $stmt_handle;
return $stmt_handle;
}
function doStatement($stmt_s, $value_s) {
$sth = $this->prepareStatement($stmt_s);
$sth->closeCursor();
$db_result = $sth->execute($value_s);
if ($db_result) {
echo "sql语句执行成功<br/>";
} else {
echo "sql语句执行失败<br/>";
}
$this->sth = $sth;
}
function fetchStatement() {
while($row = $this->sth->fetch(PDO::FETCH_ASSOC)){
foreach ($row as $key => $val) {
echo "$key -- $val ";
}
echo "<br/>";
}
}
}
$sql = "select * from price where id < ?;";
$sql2 = "insert into price (goodprice) values (?);";
$values = 21;
try{
$db = new Base();
$db->doStatement($sql, $values );
echo "";
$db->fetchStatement();
Base::$stmt[$sql]->closeCursor();
$db->doStatement($sql2, $values );
print_r(Base::$DB->lastInsertId());
echo "<br/>上次mysql操作影响" .Base::$stmt[$sql2]->rowCount() . "行"; //PDOStatement::rowCount
} catch(Exception $e) {
echo "错误" .$e->getMessage();
}
Warning: PDOStatement::execute() expects parameter 1 to be array, integer given in D:\apache2.4.10VC9\Apache24\htdocs\test\4.php on line 58
sql语句执行失败
Warning: PDOStatement::execute() expects parameter 1 to be array, integer given in D:\apache2.4.10VC9\Apache24\htdocs\test\4.php on line 58
sql语句执行失败
0
上次mysql操作影响0行
为什么php 抛出异常但继续往下执行