0

I have this select:

$stmt = $mysqli->prepare("select following from following where user =? and block=0");
$stmt->bind_param('i', $user);
$stmt->execute();
$stmt->bind_result($followers);
$stmt->fetch();
$stmt->close();

I'd like to use $followers in another mysql conection:

select... from ... where (p.user in (?))

in this ? I'd like to place the $followers variable in mysql in format. (1,5,7,2). Any ideas how to convert the bind_result $followers into this mysql in format?

RGS
  • 4,062
  • 4
  • 31
  • 67
  • 2
    Or you could simply combine the two queries using a join. Probably much faster execution and lower server load. But with out real queries to look at, and table schemas, there's no way to give you a real answer. – Sloan Thrasher May 09 '18 at 22:24

1 Answers1

1

One way to accomplish this would be to create another string variable and append the result of $followers in a while loop.

I typically work in procedural style, and I am assuming that your statement returns multiple rows into $followers:

$sql_in_string = '';   // initialize string variable as blank
while (mysqli_stmt_fetch($stmt)){
    $sql_in_string = $sql_in_string . $followers . "," ; // append value + comma
}
$sql_in_string = rtrim($sql_in_string,",");  // remove the final comma

At this point, $sql_in_string should have your values as "1,2,3" format and then you can bind that parameter to your next query.