Pal Singh
Pal Singh

Reputation: 1974

Select query fetch first row only

The $pKeyArray only prints first row from the database which meets the WHERE clause, why isn't showing all rows which meets the WHERE clause, I don't want to put it in any loop, i just need is an array of P_Key of all rows.

$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);

Upvotes: 1

Views: 2537

Answers (4)

rainecc
rainecc

Reputation: 1502

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

Upvotes: 0

ComFreek
ComFreek

Reputation: 29414

You have to use a loop because the mysqli_fetch_*() functions only returns one row per call.

Use this code:

$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
}

Or use mysqli_fetch_all():

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

Upvotes: 0

dpk2442
dpk2442

Reputation: 701

You need to call mysqli_fetch_array() for each row.

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

Upvotes: 2

nickb
nickb

Reputation: 59699

Use mysqli_fetch_all:

$array = mysqli_fetch_all( $PKeyDetails);

Upvotes: 1

Related Questions