2
我正在使用PDO用於MySQL數據庫連接,選擇,更新和刪除。具有特殊字符的PHP PDO
但我有與特殊字符選擇行,例如一個問題,我想選擇與「法官Fürstová米拉」的網頁的標題,
表page
,
id title content
1 Judge-Fürstová Mila xxx
SQL,
SELECT *
FROM page
WHERE title = 'Judge-Fürstová Mila'
如果我通過phpmyadmin查詢返回結果。
但它返回0
與PDO,
$sql = ' SELECT *
FROM page
WHERE title = ?';
$items = $connection->fetch_assoc($sql,'Judge-Fürstová Mila');
下面是我的DB類,
class database_pdo
{
# database handler
protected $connection = null;
# make a connection
public function __construct($dsn,$username,$password)
{
try
{
# MySQL with PDO_MYSQL
$this->connection = new PDO($dsn, $username, $password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
# call the get_error function
$this->get_error($e);
}
}
# don't forget to add getter method to get $this->connection, it's just a good practice.
public function get_connection()
{
return $this->connection;
}
public function fetch_assoc($query, $params = array())
{
try
{
# prepare the query
$stmt = $this->connection->prepare($query);
# if $params is not an array, let's make it array with one value of former $params
if (!is_array($params)) $params = array($params);
# execute the query
$stmt->execute($params);
# return the result
return $stmt->fetch();
}
catch (PDOException $e)
{
# call the get_error function
$this->get_error($e);
}
}
我錯過了在我的DB類或者別的什麼東西?
非常感謝爲了這!修正了這個答案!謝謝。 – laukok 2012-04-18 13:39:32