Call a PHP function inside of a while loop -


i have function gets particular set of users table particular where condition meet.

i need send each of them message.

so, used function send message. , called function inside following while loop

while($user= mysqli_fetch_assoc($users_set)){    send_message($user['email']); } 

so, problem is, function called 1 time. (only last value of loop)

how fix problem , make function called each value of loop...

this full code...

$query = "select * "; $query .= "from user "; $query .= "where confirmed = 0";  $user_set = mysqli_query($db_conx, $query); confirm_query($user_set);  while($user = mysqli_fetch_assoc($user_set)){    send_message($user['email']); } 

here send message function....

function send_message($email){ global $db_conx;  $invitee_user = get_user_by_email($email);  $query5 = "insert notification("; $query5 .= "description, user_id"; $query5 .= ") values("; $query5 .= "'you have been confirmed'"; $query5 .= ", {$invitee_user['id']}"; $query5 .= ")";  $result5 = mysqli_query($db_conx, $query5);  if($result5){     //$_session["message"] = "notification sent". \mysqli_error($db_conx);     return "ok"; }else{     //$_session["message"] = "failed send notification". mysqli_error($db_conx); } 

}

here code confirm_query()

function confirm_query($result_set){ if(!$result_set){    die("fatal error occured : database query failed <a href=\"error-report.php\">report error</a>");  } 

}

i boil down 1 query , rid of looping stuff

insert notification (description, user_id) select 'you have been confirmed', user_id user confirmed = 0 

your current logic convoluted.

you query user table user email field, pass email parameter function turn around , (i presume) user id based on email (when had information initial query), make insert.

this means every record return first query, need 2 queries insert notification table. if had 100 results end doing total of @ least 201 queries complete insertions.

using approach make 1 query regardless of how many rows affected.

one takeaway should that, anytime see trying sort of nested querying, should recognize anti-pattern (a coding pattern not want typically use). there better approach can taken if rethink how writing queries.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -