-2

我正在使用 PHP PDO 连接到 MySQL 数据库并返回结果集,但我似乎得到了重复的输出,尽管我的数据库中只有 2 条记录。

MYSQL:

mysql> SELECT * FROM nouns;
+-----+---------------+---------+
| PID | german        | english |
+-----+---------------+---------+
|   1 | die Männer    | men     |
|   2 | die Frauen    | women   |
+-----+---------------+---------+
2 rows in set (0.00 sec)

mysql> 

PHP 片段 1:

$database = new MySQLConnect();
$database->query("SELECT german, english FROM nouns;");
while ($row = $database->row()->fetch()) {
    foreach ($row as $value) {
    $data .= $value;
}

PHP 片段 2:

interface IMySQLSettings
{
    const HOSTNAME = "hostname";
    const DATABASE = "database";
    const USERNAME = "username";
    const PASSWORD = "password";

    function __construct();
    function query($query);
    function row();
}

class MySQLConnect implements IMySQLSettings
{
    private $hostname = IMySQLSettings::HOSTNAME;
    private $database = IMySQLSettings::DATABASE;
    private $username = IMySQLSettings::USERNAME;
    private $password = IMySQLSettings::PASSWORD;

    private $connect;
    private $query;

    function __construct()
    {
        $this->connect = new PDO("mysql:host=$this->hostname;dbname=$this->database", $this->username, $this->password);
        $this->connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    function query($query)
    {
        try {
            $this->query = $this->connect->prepare("$query");
            $this->query->execute();
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    function row()
    {
        return $this->query;
    }
}

输出:

die Männerdie Männermenmendie Frauendie Frauenwomenwomen
4

2 回答 2

2

在您的提取中,您必须提供 PDO::FETCH_NUM 或 PDO::FETCH_ASSOC,因为 PDO::FETCH_BOTH 是默认值。

也许当你构造 PDO 对象时添加这一行

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

更多信息PDO fetch_style

这是您从 php.net 手册中获得的内容:

PDO::FETCH_ASSOC
Array
(
    [NAME] => apple
    [COLOUR] => red
)

PDO::FETCH_BOTH
Array
(
    [NAME] => banana
    [0] => banana
    [COLOUR] => yellow
    [1] => yellow
)
于 2013-08-29T13:03:54.010 回答
0

一个重构版本,去掉了所有无用的代码,但添加了必需的功能。

class MySQLConnect
{
    private $connect;

    function __construct()
    {
        $dsn = "mysql:host=".IMySQLSettings::HOSTNAME.";dbname=".IMySQLSettings::DATABASE;
        $opt = array(
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
         );
        $this->connect = new PDO($dsn, IMySQLSettings::USERNAME,IMySQLSettings::PASSWORD, $opt);
    }

    function query($query, $params = array())
    {
        $stmt = $this->connect->prepare($query);
        return $stmt->execute($params);
    }
}

$db = new MySQLConnect();
$stmt = $db->query("SELECT german, english FROM nouns");
foreach ($stmt as $row) {
    foreach ($row as $value) {
    $data .= $value;
}

虽然我保留了 IMySQLSettings 的东西,但它作为接口完全没用。
如果我需要另一个数据库来连接怎么办?重新定义 MySQLConnect 以实现另一个接口?

它必须是一个数组,而不是花哨的接口,传递给 MySQLConnect 的构造函数。

于 2013-08-29T13:29:27.357 回答