PHP MYSQL PDO getColumnMeta

时间:2015-11-06 03:03:41

标签: php mysql mysqli pdo

抱歉,我的英语不好,我会说法语。)

目前我使用MySQLi,并尝试将我的所有MySQLi转换为PDO。

MySQL中的

我有这个代码,它工作得非常好:

// connection string in MySQLi

if ($query = $connection->prepare("SELECT u.ID as ID,
                                          u.Username as Username,
                                          u.Firstname as Firstname,
                                          u.Lastname as Lastname,
                                          // ... many more
                                     FROM Users u
                               INNER JOIN Gender g ON u.Gender = g.id
                                          // ... many more
                                    WHERE u.ID = ?")) {

                $query->bind_param('s', $_SESSION['ID']);
                $query->execute();
                $metaResults = $query->result_metadata();
                $fields = $metaResults->fetch_fields();
                $statementParams = '';

                foreach ($fields as $field) {
                    if (empty($statementParams)) {
                        $statementParams.="\$column['" . $field->name . "']";
                    } else {
                        $statementParams.=", \$column['" . $field->name . "']";
                    }
                }

                $statment = "\$query->bind_result($statementParams);";

                eval($statment);
                $query->store_result();

                $affected = $query->num_rows;
                // this request return me only ONE row

                if ($affected == 1) {
                    while ($query->fetch()) {
                        foreach ($column as $key => $value) {
                            if ($key == "lookingFor") {
                                $row_tmb[$key] = formatLookingFor($value, $language, "");
                            } else {
                                $row_tmb[$key] = utf8_encode($value);
                                $row_tmb[$key] = $value;
                            }
                        }

                        $results[] = $row_tmb;
                    }

                    $query->free_result();
                    $query->close();

                    $profileData = $results[0];
// ... other code    
}

这是返回我的所有列名和所有1个数据行,我很高兴。因此,我尝试使用新的PDO代码将此代码转换为PDO:

// good connection string without error in PDO code and the same query as you see up.
            if ($query = $connection->prepare($sql)) {
                $query->execute();
                $metaResultsColNumber = $query->columnCount();

                for ($i = 0; $i < $metaResultsColNumber; $i++) {
                    $metaResults[] = $query->getColumnMeta($i, ['name']);
                }
                var_dump($metaResults);

                $fields = $metaResults->fetchColumn();
                var_dump($fields);
                $statementParams = '';

                foreach ($fields as $field) {
                    if (empty($statementParams)) {
                        $statementParams.="\$column['" . $field->name . "']";
                    } else {
                        $statementParams.=", \$column['" . $field->name . "']";
                    }
                }
                $statment = "\$query->bind_result($statementParams);";

                eval($statment);
                $query->store_result();

                $affected = $query->num_rows;

                // TRACE
                printf("SQL %d row(s) return", $affected);

                if ($affected == 1) {
                    while ($query->fetch()) {
                        foreach ($column as $key => $value) {
                            if ($key == "lookingFor") {
                                $row_tmb[$key] = formatLookingFor($value, $language, "");
                            } else {
                                $row_tmb[$key] = utf8_encode($value);
                            }
                        }

                        $results[] = $row_tmb;
                    }

                    $query->free_result();
                    $query->close();

                    $profileData = $results[0];

我无法获得1)正确的列名称2)返回行的数据

我尝试在几个小时内阅读本网站和PHP MYSQL PDO文档的帮助。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你找那样的东西吗?

//Datastrucure 
include("pdo_dbconnect.php");
$stmt = $db->prepare('select * from information_schema.columns where table_name = "' . $_SESSION[$fenster .'_tabelle'] . '" and table_schema = "' .$database.'"');
$stmt->execute();

$f = -1;
while ($data = $stmt->fetch()) {
    $f += 1;
    //pmsg($data['COLUMN_NAME'] . ' ' .$data['DATA_TYPE'] . ' ' . $data['CHARACTER_MAXIMUM_LENGTH']);
    $_SESSION['_fieldName'][$f] = $data['COLUMN_NAME'];
    $_SESSION['_fieldLenght'][$f] = $data['CHARACTER_MAXIMUM_LENGTH'];
    $_SESSION['_extra'][$f] = $data['EXTRA'];
}