2012-06-07 92 views
4

在我的表中我有2条记录companyid = 1,但是当我运行下面的companyid = 1时,它只返回第一个!
如何获取所有记录?如何在php mysql中获取结果的所有行?

PHP文件:

if (isset($_GET["companyid"])) { 
$companyid = $_GET['companyid']; 

// get a product from products table 
$result = mysql_query("SELECT * FROM `products`   
         WHERE companyid = $companyid;"); 

if (!empty($result)) {  

    if (mysql_num_rows($result) > 0) { 

    while($row = mysql_fetch_assoc($result)){ 
     $product = array(); 
     $product["pid"] = $row["pid"]; 
     $product["productname"] = $row["productname"];   
    } 

    $response["product"] = array(); 

     array_push($response["product"], $product); 

     // success 
     $response["success"] = 1; 

    echo json_encode($response); 

    } else { 
     // no product found 
     $response["success"] = 0; 
     $response["message"] = "No product found"; 

     // echo no product JSON 
     echo json_encode($response); 
    } 
} else { 
    // no product found 
    $response["success"] = 0; 
    $response["message"] = "No product found"; 

    // echo no users JSON 
    echo json_encode($response); 
} 
} else { 
$response["success"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
} 

使用mysql_fetch_array正在发生的事情一样。 返回{"product":[{"pid":"12371","productname":"test"}],"success":1} 当我运行,而无需使用mysql_fetch_array返回所有行参数select * from table查询..

+0

你需要遍历结果并读入每一行。将您的if(mysql_num_rows ...)调用更改为for循环。 – Marvo

+0

不要执行'$ result = mysql_fetch_assoc($ result);'当你这样做时,你正在销毁你的'$ result'数据。重命名它'$行'或东西 –

+1

可能重复[基于查询获取所有行到数组中](http://stackoverflow.com/questions/9931684/fetch-all-rows-based-on-the-query -into-an-array) – nickb

回答

3

您在结果需要循环拉所有行:

while($row = mysql_fetch_assoc($result)){ 
//Do stuff 
} 

在一个侧面说明,你应该至少使用mysqli或PDO而不是mysql_ *函数。

+0

我已经编辑了上面的php代码..使用循环我仍然得到相同的结果 – mprog

7
while ($row = mysql_fetch_assoc($result)) { 
    echo $row["userid"]; 
    echo $row["fullname"]; 
    echo $row["userstatus"]; 
} 

mysql_free_result($result); 

php.net/mysql_fetch_assoc

我会建议你使用,而不是mysql_

PDO
if (!empty($result)) { 

可能是is_resource($result)

26

由于NikiC指出你不应该不再使用mysql_函数,您可以在PDO和mysqli中获取整个数组,下面是使用mysqli->fetch_all函数获取所有行的示例, 希望这可以帮助!

//Database Connection 
$sqlConn = new mysqli($hostname, $username, $password, $database); 

//Build SQL String 
$sqlString = "SELECT * FROM my_table"; 

//Execute the query and put data into a result 
$result = $this->sqlConn->query($sqlString); 

//Copy result into a associative array 
$resultArray = $result->fetch_all(MYSQLI_ASSOC); 

//Copy result into a numeric array 
$resultArray = $result->fetch_all(MYSQLI_NUM); 

//Copy result into both a associative and numeric array 
$resultArray = $result->fetch_all(MYSQLI_BOTH); 
+0

+1非常干净的代码,并解释清楚。 –

+2

fetch_all最初没有在ubuntu上工作 - 我需要做的:sudo apt-get安装php5-mysqlnd并重新启动apache – amurrell

+0

'$ result = $ this-> sqlConn-> query($ sqlString);'没有工作对我来说,我不得不使用'$ result = $ sqlConn-> query($ sqlString);' – Druzion

0

我坚信用Doctrine或任何类型的MySQL迭代(PDO或mysqli)进行批处理只是一种幻想。

@ dimitri-k提供了一个很好的解释,特别是关于工作单元。问题是导致错过:“$ query-> iterate()”,它并不真正迭代数据源。它是只是一个\ Traversable包装左右已经完全提取数据源。

证明,即使从图片完全去除主义抽象层,我们仍然会碰到内存的例子发出

echo 'Starting with memory usage: ' . memory_get_usage(true)/1024/1024 . " MB \n"; 

$pdo = new \PDO("mysql:dbname=DBNAME;host=HOST", "USER", "PW"); 
$stmt = $pdo->prepare('SELECT * FROM my_big_table LIMIT 100000'); 
$stmt->execute(); 

while ($rawCampaign = $stmt->fetch()) { 
    // echo $rawCampaign['id'] . "\n"; 
} 

echo 'Ending with memory usage: ' . memory_get_usage(true)/1024/1024 . " MB \n"; 

输出:

Starting with memory usage: 6 MB 
Ending with memory usage: 109.46875 MB 

在这里,令人失望getIterator()方法:

namespace Doctrine\DBAL\Driver\Mysqli\MysqliStatement 

/** 
* {@inheritdoc} 
*/ 
public function getIterator() 
{ 
    $data = $this->fetchAll(); 

    return new \ArrayIterator($data); 
} 

您可以使用我的小型库到实际上使用PHP Doctrine或DQL或纯粹的SQL流重型表。但是你找到合适的:https://github.com/EnchanterIO/remote-collection-stream