PHP教程:如何根据数据库中某列的唯一值来检索数据
P粉764785924
2023-08-14 11:13:09
<p>我想从数据库中检索<code>NEW_COLUMN_1</code>和<code>NEW_COLUMN_2</code>列的值,使用以下函数中<code>NUMBER</code>列中的不同CR号码:</p>
<pre class="brush:php;toolbar:false;">function retrieveDistinctFilePaths(array $distinctCRNumbers, $database)
{
$filePaths = [];
echo "<pre>"; print_r($distinctCRNumbers); echo "</pre>"; // Line A
foreach ($distinctCRNumbers as $crNumber) {
$query = "
SELECT
NEW_COLUMN_1,
NEW_COLUMN_2
FROM
your_table_name
WHERE
NUMBER = '$crNumber'";
// 执行查询并从数据库中获取结果
$database->executeStatement($query);
// 从结果中获取行
$row = $database->fetchRow();
if ($row) {
$filePaths[$crNumber] = [
'NEW_COLUMN_1' => $row['NEW_COLUMN_1'],
'NEW_COLUMN_2' => $row['NEW_COLUMN_2']
];
}
}
echo "<pre>"; print_r($filePaths); echo "</pre>"; // Line Y
return $filePaths;
}</pre>
<p>上述函数中的<strong>Line A</strong>打印如下内容:</p>
<pre class="brush:php;toolbar:false;">Array
(
[0] => AUTH-GOOD-MORNING
[1] => GOOD-MORNING
)</pre>
<p>上述函数中的<strong>Line Z</strong>输出(打印)如下内容。从<strong>Line Z</strong>的输出中可以看出,问题在于它没有根据Line A中的<code>'GOOD-MORNING'</code>数组值打印结果。</p>
<pre class="brush:php;toolbar:false;">Array
(
[AUTH-GOOD-MORNING] => Array
(
[0] => Array
(
[NEW_COLUMN_1] =>
[NEW_COLUMN_2] =>
)
)
)</pre>
<p><strong>问题陈述:</strong>我想知道我需要在上述函数中进行哪些更改,以便Line Z根据Line A中的两个数组值打印值。</p>
雷雷