简体   繁体   English

PHP仅从mysql查询返回一个结果-mysql控制台返回数十个

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

I would like to read all the data from my database, when I run this query from the console I get many results but for some reason php is only reading one. 我想从我的数据库中读取所有数据,当我从控制台运行此查询时,我得到了很多结果,但是由于某种原因,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>";

I have tried many ways of breaking the data out of the result, I have modified the query in several ways, with group by's, counts, etc trying to break this out. 我尝试了多种从结果中分解出数据的方法,我以多种方式修改了查询,例如分组依据,计数等试图将其分解。

Fairly new to joins also so if this is ugly, it's because it was the first hack that didn't spit out 12 million results. 加盟也很新,所以如果这很难看,那是因为这是第一个没有产生1200万个结果的黑客。

Try this: 尝试这个:

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

The function mysql_fetch_assoc returns a single record by default . 默认情况下,函数mysql_fetch_assoc返回一条记录。 You'll need to iterate loop as @tyralcori said.. 您需要像@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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM