search

Home  >  Q&A  >  body text

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>
P粉764785924P粉764785924501 days ago491

reply all(1)I'll reply

  • P粉461599845

    P粉4615998452023-08-15 00:40:16

    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;
    }

    reply
    0
  • Cancelreply