PHP Tutorial: How to retrieve data based on a unique value of a column in a database
<p>I want to retrieve the values of <code>NEW_COLUMN_1</code> and <code>NEW_COLUMN_2</code> columns from the database using the difference in the <code>NUMBER</code> column in the following function CR number: </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'";
//Execute the query and get the results from the database
$database->executeStatement($query);
// Get rows from results
$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> in the above function prints the following: </p>
<pre class="brush:php;toolbar:false;">Array
(
[0] => AUTH-GOOD-MORNING
[1] => GOOD-MORNING
)</pre>
<p><strong>Line Z</strong> in the above function outputs (prints) the following content. As you can see from the output of <strong>Line Z</strong>, the problem is that it is not printing the result based on the <code>'GOOD-MORNING'</code> array value in Line A. </p>
<pre class="brush:php;toolbar:false;">Array
(
[AUTH-GOOD-MORNING] => Array
(
[0] => Array
(
[NEW_COLUMN_1] =>
[NEW_COLUMN_2] =>
)
)
)</pre>
<p><strong>Problem Statement: </strong>I would like to know what changes I need to make in the above function so that Line Z prints the value based on the two array values in Line A. </p>