PHP Tutorial: How to retrieve data based on a unique value of a column in a database
P粉764785924
P粉764785924 2023-08-14 11:13:09
0
1
447
<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>
P粉764785924
P粉764785924

reply all(1)
P粉461599845
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);

        // 获取当前CR号的所有行
        $rows = $database->fetchAll();  // 获取所有行

        foreach ($rows as $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 Z
    return $filePaths;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!