I am working on building private message capability and the tutorial I am watching is based in mysql, so I am converting over to mysqli and avoiding creating an extra class for the following function the demonstrator created inside of it, so I am housing it in the same page.
I am receiving the follow error on my query
Warning: implode(): Invalid arguments passed in
My query is this...
$stmt = mysqli_query($con,"SELECT `id`, `username` FROM users WHERE username IN ('" . implode("', '", $user_names) . "')")
What am I doing wrong in my query to make this error come up? It is spot on what the demonstrator had. Could putting my function on the same page be the cause of this? Basically what he is trying to do is allow different users to be added in the 'To:' line so that it can be comma separated like.
Bob, Tim, George, etc
Full Code
function fetch_user_ids($user_names){
foreach ($user_names as &$name){
$name = mysqli_real_escape_string($name);
}
}
$con = mysqli_connect("localhost", "", "", "");
$stmt = mysqli_query($con,"SELECT `id`, `username` FROM users WHERE username IN ('" . implode("', '", $user_names) . "')")
//$stmt = $con->prepare("SELECT `id`, `username` FROM users WHERE username IN ('" . implode("', '", $user_names) . "')")
or die ("Messages query failed: %s
".($stmt->error));
//$stmt->execute();
//$stmt->bind_result($message_id, $message_username);
$names = array();
while (($row = mysqli_fetch_assoc($stmt)) !== false) {
$names[$row['username']] = $row['id'];
}
return $names;