du6jws6975 2016-04-03 16:45
浏览 35
已采纳

如何从数据库中选择存储在Array中的相同ID?

I have stored IDs as STRING of users who rated a post. Example:

$ids="5,8,4,9,7,8,87";

and I later convert the string into an array:

$kaboom = explode(',',$ids);

Now I want to query another table and select all field where the id is found in array $kaboom. I tried but I get the following error:

Notice: Array to string conversion in C:\xampp\htdocs\cebs\include\post_reacts.php on line 22

This is my code:

<?php
//include connection file
include 'dbc.php';


if(isset($_POST['post_id'])){
$post_id = $_POST['post_id'];

//SELECT ALL IDS OF THOSE WHO RATED THE POST

$query = mysqli_query($dbc_conn,"SELECT highlight_voters FROM $public_feed_table WHERE id='$post_id' LIMIT 1");

if($query==true){

while($row=mysqli_fetch_assoc($query)){
$ids = $row['highlight_voters'];
$kaboom = explode(",",$ids);


//select IDS which is/are in array and and get details about the ID

$check_ids_which_in_array = mysqli_query($dbc_conn,"SELECT * FROM $table_name WHERE id='$kaboom' ");

}

if($check_ids_which_in_array==true){
while($b=mysqli_fetch_assoc($check_ids_which_in_array)){
$person_who_rated =$b['id'];

    //do something here after...
        }

    }               
}
}
?>
  • 写回答

1条回答 默认 最新

  • douluanzhao6689 2016-04-03 16:52
    关注

    You can use IN(...)

    mysqli_query($dbc_conn, "SELECT * FROM $table_name WHERE id IN($ids)")
    

    A query that uses IN would look like this:

    SELECT * FROM table WHERE id IN(1, 2, 3, 7, 8, 9)
    

    Therefore, you don't need to explode into an array beforehand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配