4

MySQLデータベース接続にPDOを使用しており、選択、更新、削除しています。

しかし、特殊文字を含む行の選択に問題があります。たとえば、「Judge-FürstováMila」でページタイトルを選択したいのですが、

テーブル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クラスまたは他の何かを見逃したことがありますか?

4

1 に答える 1

5

に追加charset=UTF-8してみて$dsn、変更してください

$this->connection = new PDO($dsn, $username, $password);

$this->connection = new PDO($dsn, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

私はそれがそのことだと信じていますSET NAMES utf8、少なくとも私の場合はそうでした

于 2012-04-18T13:03:19.197 に答える