Sunday 5 August 2018

mysql - 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 .= '
'.$REMINDER_TIME->format('F j, Y').'
'.$REMINDER_TIME->format('g:ia T').'
'.$REMINDER_USER_FIRST.' '.$REMINDER_USER_LAST.'
('.$REMINDER_USER_EMAIL.')
';

}


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 .= '
'.$REMINDER_TIME->format('F j, Y').'
'.$REMINDER_TIME->format('g:ia T').'
'.$REMINDER_USER_FIRST.' '.$REMINDER_USER_LAST.'
('.$REMINDER_USER_EMAIL.')
';
}

$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!


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...