0

我正在尝试根据数组中包含的标识符从 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

4

6 回答 6

1

尝试:

$agent_primary[]="'".$dbc->real_escape_string($r['agent_id'])."'";
于 2013-02-07T12:00:15.580 回答
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.
}
}

用这个。我想它会对你有所帮助。

于 2013-02-07T11:33:04.700 回答
0

使用 implode 创建一个字符串并在其周围附加括号。

获取数组中 id 的值,然后将其内爆。请参见下面的示例 - 这有效。

 $arr = array(1,297,298);
    $new = '(';
    $new .= implode(',',$arr);
    $new .= ')';
    $query = "Select * from wp_posts where ID IN $new";
于 2013-02-07T11:46:13.547 回答
0

编辑:两个查询都可以与 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:独立的例子

<?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 对象
errorand属性应该包含有关错误的更详细信息。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(...)完成调试后删除该行。

于 2013-02-07T11:48:31.933 回答
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.
    }
}

希望这可以帮助

于 2013-02-07T11:50:48.270 回答
0

试试这个

 $ids=join("','", $agent_primary);
 $query5="SELECT * FROM detail_db WHERE user_id IN('$ids')";

我在我的本地主机上试过了,效果很好!

于 2013-02-07T12:48:50.710 回答