PHP只从mysql查询返回一个结果——mysql控制台返回几十个结果


PHP only returns one result from a mysql query - mysql console returns dozens

我想从我的数据库中读取所有数据,当我从控制台运行这个查询时,我得到了很多结果,但由于某些原因,php只读取了一个。

$query = "
   SELECT b.raw as 'address', a.raw as 'name', c.TouchTime as 'time'
   FROM touchName a, touchHome b, trackTouch c
   WHERE a.raw like '"%{$name}%'" 
      AND c.AgentID = 1
      AND a.relations = b.relations
      AND b.relations = c.relations
      AND a.relations = c.relations
   ORDER BY time desc
";
//So we can double check in the console
echo $query . "<br><br>";
$result = mysqli_query($mc, $query);
$array = mysqli_fetch_assoc($result);
//says there is only one row
$total = count( mysqli_num_rows($result) );
echo $total."<br>";

我已经尝试了很多方法将数据从结果中分离出来,我已经用几种方法修改了查询,groupby、count等试图将其分离出来。

加入也相当新鲜,所以如果这很难看,那是因为这是第一次没有产生1200万个结果的黑客攻击。

试试这个:

$array = array();
while ($row = mysql_fetch_assoc($result)) {
   $array[] = $row;
}

函数mysql_fetch_assoc默认返回一条记录。您需要像@tyralcori所说的那样迭代循环。。

$query = "
   SELECT b.raw as 'address', a.raw as 'name', c.TouchTime as 'time'
   FROM touchName a, touchHome b, trackTouch c
   WHERE a.raw like '"%{$name}%'" 
      AND c.AgentID = 1
      AND a.relations = b.relations
      AND b.relations = c.relations
      AND a.relations = c.relations
   ORDER BY time desc
";
echo $query;
$handler= mysqli_query($mc, $query);
$result = array();  
while($rec = mysqli_fetch_assoc($handler)){
    $result[]['address'] = $rec['address'];
    $result[]['name'] = $rec['name'];
    $result[]['time'] = $rec['time'];
}
print_r($result);