設置 | 登錄 | 註冊

作者共發了64篇帖子。

今天我来系统地学习一下PDO

42樓 巨大八爪鱼 2015-5-30 22:12
$stmt->bindColumn("ItemSuburb", $suburb, PDO::PARAM_STR);
相当于$suburb = (string)$row["ItemSuburb"];
43樓 巨大八爪鱼 2015-5-30 22:21
回复:33楼
为了排查SQL的错误,可以使用errorInfo直接获取MySQL输出的错误信息,不用再把输出的SQL语句复制到PMA中执行了。
比如:
$sql = "SELECT * AFROM WiFiHotSpots";
$dbh->query($sql);
$error = $dbh->errorInfo();
echo $error[2];
输出:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AFROM WiFiHotSpots' at line 1

44樓 巨大八爪鱼 2015-5-30 22:22
回复:43楼
这样我们就可以直接看出这个SQL语句中的AFROM出问题了
45樓 巨大八爪鱼 2015-5-30 22:25
bindParam用于给SQL语句中带有冒号或问号的占位符赋值。
http://php.net/manual/en/pdostatement.bindparam.php
46樓 巨大八爪鱼 2015-5-30 22:27
方法 bindParam() 和 bindValue() 非常相似。
 唯一的区别就是前者使用一个PHP变量绑定参数,而后者使用一个值。
所以使用bindParam是第二个参数只能用变量名,而不能用变量值,而bindValue至可以使用具体值。
http://www.360doc.com/content/12/0703/16/10337467_222033042.shtml
48樓 巨大八爪鱼 2015-5-30 22:29
fetch();和mysql_fetch_array();一样
但fetchAll();返回一个二维数组,可以获取所有行。
49樓 巨大八爪鱼 2015-5-30 22:31
以后要排查sql的错误直接用errorInfo,不要再把SQL语句复制到PMA里执行了。而且也没有办法获得最终的SQL语句。
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行。

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。