简体   繁体   中英

query result array in loop for another query

I Queried Database Table 'users' for 'user_id'. and get an array of ids.

$sel = "SELECT user_id FROM users WHERE status='Approved'";     
$result = @mysqli_query ($dbcon, $sel); 

Then i inserted values into another table income for all those user ids.

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
{
$ins = "INSERT INTO income (user_id, income_amount)  VALUES ('$row', '100')";       
$giv = @mysqli_query ($dbcon, $ins);
}

Notice: Array to string conversion in E:\\xampp\\htdocs\\project\\t.php on line 109

Can anyone help me resolve this issue.

$sel = "SELECT user_id FROM users WHERE status='Approved'";     
$result = @mysqli_query ($dbcon, $sel); 

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
{
$ins = "INSERT INTO income (user_id, income_amount)  VALUES ('" . $row['user_id'] . "', '100')";       
$giv = @mysqli_query ($dbcon, $ins);
}

First , Check if $results is in array ..you can put some error handling checked is_array($result).

If it is fine then pass it to mysqli_fetch_array(). Do't add suppress @ error ,while developing.

i would like to suggest you a single query for that so after that you need not to use while loop to insert your data in income table:

Just try it :

INSERT INTO income (user_id,income_amount) SELECT user_id,'100' AS income_amount FROM users WHERE status = 'Approved';

You can use it like that way :

$sel = "INSERT INTO income (user_id,income_amount) SELECT user_id,'100' AS income_amount FROM users WHERE status = 'Approved'";     
$result = @mysqli_query ($dbcon, $sel); 

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