我正在尝试根据数组中包含的标识符从MYSQL数据库中获取记录。要获取标识符,我的代码如下:
$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'];
}
使用print_r
访问数组详细信息时,这似乎工作正常。
我的下一个失败的陈述如下:
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.
}
}
这只会触发die()
语句。我尝试使用implode
代替join
但没有成功。
编辑:
失败查询后的die($dbc->error)
显示错误为:unknown column '' in where clause
答案 0 :(得分:1)
尝试:
$agent_primary[]="'".$dbc->real_escape_string($r['agent_id'])."'";
答案 1 :(得分:0)
$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']);
}
然后
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.
}
}
使用它。我认为它会对你有帮助。
答案 2 :(得分:0)
使用implode创建一个字符串并在其周围添加括号。
获取数组中的id值,然后将其内爆。 请看下面的示例 - 这可行。
$arr = array(1,297,298);
$new = '(';
$new .= implode(',',$arr);
$new .= ')';
$query = "Select * from wp_posts where ID IN $new";
答案 3 :(得分:0)
编辑:两个查询都可以与JOIN stament结合使用
$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:自包含示例
<?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);
}
}
打印
1 fooxy1
3 fooxy2
5 fooxy3
原始答案:
mysqli::query
返回false表示执行查询时发生错误。那可能是例如语法错误或权限或...或...
$ dbc对象的error
和errno
属性应包含有关错误的更详细信息。但是,您不应该向任何用户显示完整的错误消息
因此,出于调试目的,定义类似
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>';
}
}
然后在您的代码中使用此函数,如
<?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");
在完成调试后删除define(...)
行。
答案 4 :(得分:0)
你可以这样做
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.
}
}
希望这有助于
答案 5 :(得分:0)
试试这个
$ids=join("','", $agent_primary);
$query5="SELECT * FROM detail_db WHERE user_id IN('$ids')";
我在我的localhost中尝试过它,它完美无缺!