|
51樓 巨大八爪鱼
2015-5-31 08:15
我还是要硬着头皮看英文版的PHP文档,虽然阅读速度比中文慢了很多,但这是一个适应的过程。
|
|
52樓 巨大八爪鱼
2015-5-31 08:16
PDO — The PDO class
PDO::beginTransaction — Initiates a transaction PDO::commit — Commits a transaction PDO::__construct — Creates a PDO instance representing a connection to a database PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle PDO::exec — Execute an SQL statement and return the number of affected rows PDO::getAttribute — Retrieve a database connection attribute PDO::getAvailableDrivers — Return an array of available PDO drivers PDO::inTransaction — Checks if inside a transaction PDO::lastInsertId — Returns the ID of the last inserted row or sequence value PDO::prepare — Prepares a statement for execution and returns a statement object PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object PDO::quote — Quotes a string for use in a query. PDO::rollBack — Rolls back a transaction PDO::setAttribute — Set an attribute PDOStatement — The PDOStatement class
PDOStatement::bindColumn — Bind a column to a PHP variable PDOStatement::bindParam — Binds a parameter to the specified variable name PDOStatement::bindValue — Binds a value to a parameter PDOStatement::closeCursor — Closes the cursor, enabling the statement to be executed again. PDOStatement::columnCount — Returns the number of columns in the result set PDOStatement::debugDumpParams — Dump an SQL prepared command PDOStatement::errorCode — Fetch the SQLSTATE associated with the last operation on the statement handle PDOStatement::errorInfo — Fetch extended error information associated with the last operation on the statement handle PDOStatement::execute — Executes a prepared statement PDOStatement::fetch — Fetches the next row from a result set PDOStatement::fetchAll — Returns an array containing all of the result set rows PDOStatement::fetchColumn — Returns a single column from the next row of a result set PDOStatement::fetchObject — Fetches the next row and returns it as an object. PDOStatement::getAttribute — Retrieve a statement attribute PDOStatement::getColumnMeta — Returns metadata for a column in a result set PDOStatement::nextRowset — Advances to the next rowset in a multi-rowset statement handle PDOStatement::rowCount — Returns the number of rows affected by the last SQL statement PDOStatement::setAttribute — Set a statement attribute PDOStatement::setFetchMode — Set the default fetch mode for this statement
|
|
53樓 巨大八爪鱼
2015-5-31 08:56
exec的用法: $sql = "INSERT INTO `timetest` VALUES (null, NOW())"; $num = $dbh->exec($sql); echo "影响了{$num}行。";
输出:影响了1行。
|
|
54樓 巨大八爪鱼
2015-5-31 08:58
$sql = "INSERT INTO `timetest` VALUES (null, NOW())"; $successful = $dbh->exec($sql); if ($successful) { echo "插入记录成功"; }
|
|
55樓 巨大八爪鱼
2015-5-31 08:59
回复:54楼 这段代码如果改写成mysql原来的代码,就很复杂,除了执行mysql_query($sql),还需要执行mysql_affected_rows() 因为对于插入、删除、修改的SQL语句,mysql_query始终返回true
|
|
56樓 巨大八爪鱼
2015-5-31 09:03
PDO::query() returns a PDOStatement object, or FALSE
on failure. query主要用于SELECT语句,返回PDOStatement对象
|
|
57樓 巨大八爪鱼
2015-5-31 09:14
$content = join(", ", PDO::getAvailableDrivers()); if (!empty($content)) { $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())"; $stmt = $dbh->prepare($sql); $stmt->bindParam(":content", $content); $successful = $stmt->execute(); if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}条记录。"; } else { echo "插入记录失败!"; } } else { echo "没有内容"; }
|
|
58樓 巨大八爪鱼
2015-5-31 09:17
$content = join(", ", PDO::getAvailableDrivers()); if (!empty($content)) { $sql = "SELECT * FROM Contents WHERE Content = ?"; $stmt = $dbh->prepare($sql); $stmt->execute(array($content)); if ($stmt->rowCount()){ echo "数据表中已经有相同的内容了。"; } else { $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())"; $stmt = $dbh->prepare($sql); $stmt->bindParam(":content", $content); $successful = $stmt->execute(); if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}条记录。"; } else { echo "插入记录失败!"; } } } else { echo "没有内容"; }
|
|
59樓 巨大八爪鱼
2015-5-31 09:19
bindValue的用法 $sql = "SELECT * FROM Contents WHERE Content = ?"; $stmt = $dbh->prepare($sql); $stmt->bindValue(1, $content); $stmt->execute();
|
|
60樓 巨大八爪鱼
2015-5-31 09:24
对于$stmt->queryString这个属性,输出内容如下: INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW()) 也就是语句中仍然没有包含具体的值。
if ($successful) { $num = $stmt->rowCount(); echo "插入了{$num}条记录。<br>"; echo $stmt->queryString; } else { echo "插入记录失败!"; }
|