如何在php mysql中获取结果的所有行?

时间:2012-06-07 21:46:12

标签: php mysql json

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

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} 当我使用select * from table运行不带参数mysql_fetch_array的查询时,它会返回所有行..

4 个答案:

答案 0 :(得分:31)

由于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 = $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);

答案 1 :(得分:8)

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

mysql_free_result($result);

php.net/mysql_fetch_assoc

我建议您使用PDO而不是mysql _

if (!empty($result)) {

可以是is_resource($result)

答案 2 :(得分:4)

您需要遍历结果以拉出所有行:

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

另外,你应该至少使用mysqli或PDO而不是mysql_ *函数。

答案 3 :(得分:1)

我坚信使用Doctrine进行批处理或使用MySQL(PDO或mysqli)进行任何迭代都只是一种错觉。

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

一个例子表明即使从图片中完全删除Doctrine抽象层,我们仍会遇到内存问题

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