简体   繁体   中英

PHP MySQLi Query within Prepared Statement

I'm in the process of updating from MySQL to MySQLi and have come across an issue I'm not sure how to handle with MySQLi/prepared statements.

In my previous code, I had a query within a query:

while ($record = mysql_fetch_array($result)) 
{
    //CLEAR VARIABLES
    $REMINDER_TIME = '';
    $REMINDER_USER_ID = '';

    //STORE DATA
    $REMINDER_TIME = $record['REMINDER_TIME'];
    $REMINDER_USER_ID = $record['USER_ID'];

    //LOCALIZE TIMES    
    $REMINDER_TIME = new DateTime($REMINDER_TIME);
    $REMINDER_TIME->setTimeZone(new DateTimeZone($user_timezone));

    //GET USER NAME
    $result1 = mysql_query("SELECT FIRST_NAME, LAST_NAME, EMAIL FROM TABLE_users WHERE USER_ID='$REMINDER_USER_ID'");

    while ($record1 = mysql_fetch_array($result1)) 
    {
        //CLEAR VARIABLES
        $REMINDER_USER_FIRST = '';
        $REMINDER_USER_LAST = '';
        $REMINDER_USER_EMAIL = '';

        //STORE DATA
        $REMINDER_USER_FIRST = $record1['FIRST_NAME'];
        $REMINDER_USER_LAST = $record1['LAST_NAME'];
        $REMINDER_USER_EMAIL = $record1['EMAIL'];
    }

    $tableRow .= '<tr>
                 <td class="tall">'.$REMINDER_TIME->format('F j, Y').'<br />'.$REMINDER_TIME->format('g:ia T').'</a></td>
                 <td class="tall">'.$REMINDER_USER_FIRST.' '.$REMINDER_USER_LAST.'<br />('.$REMINDER_USER_EMAIL.')</td>
                 </tr>';
}

However, with my prepared statements, I'm not sure how to run a query within a query:

$result = $stmt -> get_result();

while ($row = $result -> fetch_array(MYSQLI_ASSOC))
{
    //CLEAR VARIABLES
    $REMINDER_TIME = '';
    $REMINDER_USER_ID = '';

    //STORE DATA
    $REMINDER_TIME = $row['REMINDER_TIME'];
    $REMINDER_USER_ID = $row['USER_ID'];

    //LOCALIZE TIMES    
    $REMINDER_TIME = new DateTime($REMINDER_TIME);
    $REMINDER_TIME->setTimeZone(new DateTimeZone($user_timezone));

    //_____QUERY WOULD GO HERE_____

    $tableRow .=    '<tr>
    <td class="tall">'.$REMINDER_TIME->format('F j, Y').'<br />'.$REMINDER_TIME->format('g:ia T').'</a></td>
    <td class="tall">'.$REMINDER_USER_FIRST.' '.$REMINDER_USER_LAST.'<br />('.$REMINDER_USER_EMAIL.')</td>
    </tr>';
}

$stmt->close();

Is it possible to do this with prepared statements? I basically need to hit the database to get data in a secondary query based on values received during the first query. Thank you!

Executing queries inside a loop is a bad practice and should be avoided, because of performance issues.

Given the code and query you posted, I suggest you to modify your first sql statement:

SELECT a.REMINDER_TIME, a.USER_ID, b.FIRST_NAME, b.LAST_NAME, b.EMAIL
FROM TABLE_logs_reminders a
LEFT JOIN table_users b on a.user_id = b.user_id
WHERE REQUEST_ID=?
ORDER BY a.REMINDER_TIME

This will fetch the data you need, in one single request, avoiding several calls to your DBMS. Then, in your php code, you can access the user columns without a new query:

//_____QUERY WOULD GO HERE_____
// No need for a query
$REMINDER_USER_FIRST = $record1['FIRST_NAME'];
$REMINDER_USER_LAST = $record1['LAST_NAME'];
$REMINDER_USER_EMAIL = $record1['EMAIL'];

There are some good tutorials about joins you should read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM