[英]MYSQL IN Statement syntax not working
I am trying to obtain records from a MYSQL database based on identifiers contained in an array. 我试图基于数组中包含的标识符从MYSQL数据库获取记录。 To get the identifiers my code is as follows:
要获取标识符,我的代码如下:
$query="SELECT agent_id FROM agent_coverage WHERE primary_area LIKE '$search_term%'";
$result=mysqli_query($dbc, $query) or die("Could not obtain primary area coverage2");
while($r=mysqli_fetch_array($result))
{
$agent_primary[]=$r['agent_id'];
}
This seems to work fine when using print_r
to access the array details. 当使用
print_r
访问数组详细信息时,这似乎工作正常。
My next statement which is the one that fails is as follows: 我的下一个失败的语句如下:
if(!empty($agent_primary))
{
$ids=join(",", $agent_primary);
$query5="SELECT * FROM detail_db WHERE user_id IN($ids)";
$result5=mysqli_query($dbc, $query5) or die("Could not do SELECT");
while($row=mysqli_fetch_array($result5))
{
//do stuff with results here.
}
}
This just fires the die()
statement. 这只会触发
die()
语句。 I have tried using implode
instead of join
with no success. 我尝试使用
implode
而不是join
而没有成功。
edit: 编辑:
die($dbc->error)
after the failing query reveals the error to be: unknown column '' in where clause
查询失败后,
die($dbc->error)
显示错误为: unknown column '' in where clause
尝试:
$agent_primary[]="'".$dbc->real_escape_string($r['agent_id'])."'";
$query="SELECT agent_id FROM agent_coverage WHERE primary_area LIKE '$search_term%'";
$result=mysqli_query($dbc, $query) or die("Could not obtain primary area coverage2");
$agent_primary=array();
while($r=mysqli_fetch_array($result))
{
array_push($agent_primary,$r['agent_id']);
}
then 然后
if(!empty($agent_primary))
{
$ids=$agent_primary;
$query5="SELECT * FROM detail_db WHERE user_id IN($ids)";
$result5=mysqli_query($dbc, $query5) or die("Could not do SELECT");
while($row=mysqli_fetch_array($result5))
{
//do stuff with results here.
}
}
use this. 用这个。 I think it will help you.
我认为它将为您提供帮助。
Use implode to create a string and append brackets around it. 使用implode创建一个字符串,并在其周围附加括号。
Get values of ids in a array and then implode it. 获取数组中id的值,然后将其内爆。 Just see below example - This works.
请看下面的示例-这可以工作。
$arr = array(1,297,298);
$new = '(';
$new .= implode(',',$arr);
$new .= ')';
$query = "Select * from wp_posts where ID IN $new";
edit: Both queries can be combined with a JOIN stament 编辑:两个查询都可以与JOIN语句组合
$query = "
SELECT
dd.user_id,
dd.foo
FROM
tmp_agent_coverage as ac
LEFT JOIN
tmp_detail_db as dd
ON
ac.agent_id=dd.user_id
WHERE
ac.primary_area LIKE '$search_term%'
";
$result=mysqli_query($dbc, $query) or mysqliError($dbc, "Could not obtain primary area coverage2");
while($r=mysqli_fetch_array($result))
{
echo $r['user_id'], ' ', $r['foo'], "\n";
}
edit2: self-contained example edit2:独立的示例
<?php
$dbc = new mysqli('localhost', 'localonly', 'localonly', 'test');
if ($dbc->connect_error) {
var_dump($mysqli->connect_errno, $mysqli->connect_error);
die;
}
setup($dbc);
$search_term = 'xy';
$query = "
SELECT
dd.user_id,
dd.foo
FROM
tmp_agent_coverage as ac
LEFT JOIN
tmp_detail_db as dd
ON
ac.agent_id=dd.user_id
WHERE
ac.primary_area LIKE '$search_term%'
";
$result=mysqli_query($dbc, $query) or mysqliError($dbc, "Could not obtain primary area coverage2");
while($r=mysqli_fetch_array($result))
{
echo $r['user_id'], ' ', $r['foo'], "\n";
}
define('DEVELOPMENT_DEBUG_MESSAGES', 1);
function mysqliError($dbc, $description) {
if ( !defined('DEVELOPMENT_DEBUG_MESSAGES') || !DEVELOPMENT_DEBUG_MESSAGES ) {
echo '<div class="error">', htmlspecialchars($description), '</div>';
}
else {
echo '<fieldset class="error"><legend>', htmlspecialchars($description), '</legend>',
htmlspecialchars($dbc->error), '</div>';
}
}
function setup($dbc) {
$q = array(
'CREATE TEMPORARY TABLE tmp_agent_coverage (
agent_id int auto_increment,
primary_area varchar(32),
primary key(agent_id),
key(primary_area)
)',
"INSERT INTO tmp_agent_coverage (primary_area) VALUES ('xy1'),('dfg'),('xy2'),('abc'),('xy3')",
'CREATE TEMPORARY TABLE tmp_detail_db (
user_id int auto_increment,
foo varchar(32),
primary key(user_id)
)',
"INSERT INTO tmp_detail_db (foo) VALUES ('fooxy1'),('foodfg'),('fooxy2'),('fooabc'),('fooxy3')"
);
foreach($q as $query) {
$dbc->query($query) or die(__LINE__ .' '.$query. ' '. $dbc->error);
}
}
prints 版画
1 fooxy1
3 fooxy2
5 fooxy3
original answer: 原始答案:
mysqli::query
returning false means that an error occured while executing the query. mysqli::query
返回false表示执行查询时发生错误。 That could be eg a syntax error or privileges or ... or ... 例如,可能是语法错误或特权或...或...
The error
and errno
properties of your $dbc object should hold more detailed information about the error. $ dbc对象的
error
和errno
属性应包含有关该错误的更多详细信息。 But you shouldn't show the complete error message to just any arbitrary user. 但是,您不应仅向任何任意用户显示完整的错误消息。
So, for debugging purposes define a function like 因此,出于调试目的,请定义一个类似
define('DEVELOPMENT_DEBUG_MESSAGES', 1);
function mysqliError($dbc, $description) {
if ( !defined('DEVELOPMENT_DEBUG_MESSAGES') || !DEVELOPMENT_DEBUG_MESSAGES ) {
echo '<div class="error">', htmlspecialchars($description), '</div>';
}
else {
echo '<fieldset class="error"><legend>', htmlspecialchars($description), '</legend>',
htmlspecialchars($dbc->error), '</div>';
}
}
and then use this function in your code like 然后在您的代码中使用此功能,例如
<?php
define('DEVELOPMENT_DEBUG_MESSAGES', 1);
[...]
$query="SELECT agent_id FROM agent_coverage WHERE primary_area LIKE '$search_term%'";
$result=mysqli_query($dbc, $query) or mysqliError($dbc, "Could not obtain primary area coverage2");
remove the define(...)
line when you're done debugging. 完成调试后,删除
define(...)
行。
You can do it like this 你可以这样
if(!empty($agent_primary))
{
$ids=implode($agent_primary,",");
$query5="SELECT * FROM detail_db WHERE user_id IN ($ids)";
$result5=mysqli_query($dbc, $query5) or die("Could not do SELECT");
while($row=mysqli_fetch_array($result5))
{
//do stuff with results here.
}
}
Hope this Helps 希望这可以帮助
try this 尝试这个
$ids=join("','", $agent_primary);
$query5="SELECT * FROM detail_db WHERE user_id IN('$ids')";
i tried it in my localhost and it works perfect! 我在我的本地主机中尝试了它,并且效果很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.