I have already used mysql queries with an IN()
clause but now I have a problem. So, I have a database with users and I want to search for those who is in the $namesarr
array. Unfortunately it does not work. I checked it many times, searched for errors but nothing. It does not give back any rows (num_rows = 0
).
Code:
// Get names
$incommnames = "";
$namesarr_ = join("','", $incomm); // join each item with a comma
$namesarr = str_replace("'", "", $namesarr_); // remove any single quotes
$sql = "SELECT username FROM users WHERE username IN (?)"; // but query does not work
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$namesarr);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
$uname = $row["username"];
$incommnames .= '<a href="user.php?u='.$uname.'">'.$uname.'</a>';
}
$stmt->close();
However if I echo the $namesarr_
I get a string like username1,username2 ...
so it would work fine but it doesn't. And if I only have one row affected I have no problems. So, what is wrong?
Edit:
$incomm = array_intersect($myf, $theirf);
$myf
is the my friends array and $theirf
is the their friends array. $incomm
is the part in common.