Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQL连接结果不断重复_Php_Mysql_Arrays - Fatal编程技术网

Php SQL连接结果不断重复

Php SQL连接结果不断重复,php,mysql,arrays,Php,Mysql,Arrays,在我的数据库中有3个表:user、phone和number。因此,它们的结构如下: ID | name ---+------ 1 | Joe 2 | Gin 3 | Ash ID | Brand | series ---+-----------+-------- 1 | Samsung | s7 2 | Iphone | 6s 3 | Samsung | s5 ID | number ---+---------- 1 | 77612 2

在我的数据库中有3个表:user、phone和number。因此,它们的结构如下:

ID | name
---+------
 1 | Joe
 2 | Gin
 3 | Ash


ID |   Brand   | series
---+-----------+--------
 1 |  Samsung  | s7
 2 |  Iphone   | 6s
 3 |  Samsung  | s5


ID |  number   
---+----------
 1 |  77612  
 2 |  34014  
 3 |  98271
我要做的是使用JOIN选择。以下是我的尝试:

$query = "SELECT u.id, p.brand, n.number
          FROM `user` u 
          LEFT OUTER JOIN `phone` p
              ON u.id = p.id
          LEFT OUTER JOIN `number` n 
              ON p.id = n.id
          WHERE u.id = '$selected'";

$sql = mysql_query($query);

if ($sql === FALSE) { 
    die(mysql_error());
}

while ($result = mysql_fetch_row($sql)) {
    $final[] = $result;
    echo '<pre>';
    print_r($final);
    echo '</pre>';
}
但结果是:

$selected =  array(array(1), array(2), array(3));

// make list of id from source array
$selected = call_user_func_array('array_merge', $selected);
$selected = implode(',', $selected); // 1,2,3  
如果我们设置$selected=array1、2、3,输出将与上面所示的相同。我怎样才能解决这个问题

WHERE u.id in ($selected)
并将where子句更改为

function implode_r($g, $p) {
    return is_array($p) ?
           implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) : 
           $p;
}

首先,你必须用逗号分隔你的ID。为此,我使用中的以下函数

然后,您必须使用关键字稍微更改查询:


你好,谢谢你的回复。。我在$selected数组结构上出错。在这里,我已经编辑了它$selected=array1,2,3:顺便说一下,我试过你的,看起来array\u merge:Argument 1不是…中的数组。在这种情况下,不要使用第1行。just$selected=内爆“,”,$selected;//1,2,3我试过了,但在“where子句”中出现了一个未知的错误列“1”,D:这里是选择u.id,p.brand,n.number FROM user u LEFT OUTER JOIN phone p ON u.id=p.id LEFT OUTER JOIN number n ON p.id=n.id where u.id u.id in 1,2 where u.id u.id in 1,2必须是u.id in 1,2的位置您好,谢谢您的回复。。我在$selected数组结构上出错。在这里,我已经编辑了$selected=array1,2,3:然后可以使用简单的内爆函数$ids=infrade,,$selected.Yup,我已经尝试过了,但是在where子句@kiriakito中出现了未知列“1”,表中的ids是什么类型的。它们应该是整数。我猜它们是弦
WHERE u.id in ($selected)
function implode_r($g, $p) {
    return is_array($p) ?
           implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) : 
           $p;
}
$ids=implode_r(",",$selected);

$query = "SELECT u.id, p.brand, n.number FROM `user` u 
LEFT OUTER JOIN `phone` p ON u.id = p.id LEFT OUTER JOIN `number` n 
ON p.id = n.id WHERE u.id in ($ids)";