gpt4 book ai didi

php - 如何使用getColumnMeta?

转载 作者:行者123 更新时间:2023-11-29 21:51:55 24 4
gpt4 key购买 nike

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

在 MySQLi 中,我有这段代码,它工作得很好:

// 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 文档。

最佳答案

你在寻找类似的东西吗?

//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'];
}

关于php - 如何使用getColumnMeta?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33558752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com