简体   繁体   中英

Selecting an array of rows using MySQLi

I'm trying to return results from an array of varying row numbers. Right now each row number in mysqli query is manually entered. How would you replace these values with the values of the currentUsers array?

$currentUsers = array('1', '3', '7', '10');

while ($row = mysqli_fetch_array ($query))
{   
    $interestValue = ((int) "$row[1]") + 
                     ((int) "$row[3]") + 
                     ((int) "$row[7]") + 
                     ((int) "$row[10]");
    echo "$interestValue";
}

Inside the while loop use the array like this:

$interestValue = 0;
foreach ( $currentUsers as $i ) {
    $interestValue = $interestValue + (int) $row[$i];
}
echo $interestValue;

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