[英]Create MySQL query with IDs from results
I have aa form which let's the user select what they want to display.我有一个表单,让用户选择他们想要显示的内容。 Now the results of this looks like that: 9, 10, 11
.现在这个结果看起来像这样: 9, 10, 11
。 These are IDs from the table.这些是表中的 ID。
These are the IDs of the type they what to show.这些是它们要显示的类型的 ID。 I have my query already, but I want to add this part at the end of my query.我已经有了我的查询,但我想在查询的末尾添加这部分。
So in this case:所以在这种情况下:
$query = "type_ID = $result1 or type_ID = $result2 or type_ID = $result3"
if printed out with echo:如果用 echo 打印出来:
type_ID = 9 or type_ID = 10 or type_ID = 11
How can I achieve this?我怎样才能做到这一点?
I tried to loop and it,however this did not work and I am confused how to do add the MySQL code to this.我试图循环它,但是这不起作用,我很困惑如何将 MySQL 代码添加到它。
$result = $result . $_GET['type_ID'][$i]
I'm a tad unsure of what it is you are trying to achieve but from what I understand this should do it:我有点不确定您想要实现的目标,但据我所知,这应该做到:
<?php
$tID = $_GET['type_ID'];
$query = 'SELECT * FROM table WHERE';
$i = 0;
foreach($tID AS $id){
if($i == 0){
$query .= ' type_ID = ' . $id;
$i++;
}else{
$query .= ' OR type_ID = ' . $id;
}
}
Although if you are only looking for the type_ID I'd still recommend using IN()
like so:虽然如果你只是在寻找 type_ID 我仍然建议使用IN()
像这样:
<?php
$tID = $_GET['type_ID'];
$query = "SELECT * FROM table WHERE type_ID IN (" . implode(',',$tID) . ")";
In works just like OR, just instead of having to write multiple OR
you can just use a single IN()
:)在工作中就像 OR,只需使用单个IN()
即可,而不必编写多个OR
你试过这个吗?
$sql = "SELECT * FROM table WHERE ".$query;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.