简体   繁体   English

PHP和MySQL查询问题

[英]PHP & MySQL query question

How can I check each value in each array in my MySQL query. 如何在MySQL查询中检查每个数组中的每个值。 I hope this makes sense. 我希望这是有道理的。

SELECT articles_comments.comment_id FROM articles_comments
WHERE articles_comments.comment_id =

Array
(
    [0] => 5
    [1] => 6
    [2] => 10
    [3] => 11
    [4] => 12
    [5] => 17
    [6] => 3
    [7] => 4
    [8] => 7
    [9] => 8
    [10] => 9
    [11] => 16
)

AND articles_comments.user_id = 

Array
(
    [2] => 4
    [3] => 4
    [4] => 4
    [8] => 4
    [9] => 4
    [10] => 4
)

Like that : 像那样 :

$query = "SELECT articles_comments.comment_id FROM articles_comments
WHERE articles_comments.comment_id IN(" . implode(",", $commentArray) . ")
  AND articles_comments.user_id IN(". implode(",", $userArray) . ")";

?

SELECT articles_comments.comment_id 
FROM articles_comments
WHERE articles_comments.comment_id in (1,2,3,4,5)
AND articles_comments.user_id in (6,7,8,9)

Try 尝试

<?php  

$arr1 = Array(5,6,10,11,12,17,3,4,7,8,9,16);

$arr2 = Array(4,4,4,4,4,4);


$sql = "SELECT articles_comments.comment_id FROM articles_comments
WHERE articles_comments.comment_id IN ("
.implode(",",$arr1).
")
AND articles_comments.user_id IN ("
.implode(",",$arr2).
") ";

echo $sql;

// You still need to connect to a database, execute this query and display the results
// This is just how you would parse the array into the query

?>

It gives this for the SQL: 它为SQL提供了此功能:

SELECT articles_comments.comment_id FROM articles_comments WHERE
articles_comments.comment_id IN (5,6,10,11,12,17,3,4,7,8,9,16) AND
articles_comments.user_id IN (4,4,4,4,4,4) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM