选择查询仅获取第一行

时间:2012-05-08 17:49:44

标签: php mysql

$ pKeyArray只打印满足WHERE子句的数据库的第一行,为什么不显示符合WHERE子句的所有行,我不想把它放在任何循环中,我只需要一个数组所有行的P_Key。

$getPKey = "SELECT P_Key FROM likersTable WHERE executed=0";
$PKeyDetails = mysqli_query($dbConnect,$getPKey)
                or die('Some error in post id');
$pKeyArray = mysqli_fetch_array($PKeyDetails);
print_r($pKeyArray);

4 个答案:

答案 0 :(得分:2)

您需要为每一行调用mysqli_fetch_array()

while ($pKeyArray = mysqli_fetch_array($PKeyDetails)) {
    print_r($pKeyArray);
}

答案 1 :(得分:1)

使用mysqli_fetch_all

$array = mysqli_fetch_all( $PKeyDetails);

答案 2 :(得分:0)

您必须使用循环,因为mysqli_fetch_*()函数每次调用只返回一行。

使用此代码:

$getPKey = "SELECT P_Key FROM likersTable WHERE executed=0";
$PKeyDetails = mysqli_query($dbConnect,$getPKey)
                or die('Some error in post id');

while ($row=mysqli_fetch_array($PKeyDetails))
{
  // Do something with $row
}

或使用mysqli_fetch_all()

$result = mysqli_fetch_all($PKeyDetails, MYSQLI_ASSOC); // or use MYSQLI_NUM

答案 3 :(得分:0)

while($pKeyArray = mysqli_fetch_array($PKeyDetails)) {
    print_r($pKeyArray);
}