1

I have a database in MySQL and I'm using this query to select certain rows from it using PHP:

$q = "SELECT Number, Body 
      FROM boxes 
      WHERE Number BETWEEN '1' AND '4' ORDER BY Number ASC";

Then calling the query and initiating arrays:

$r = $mysqli->query($q);
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
$array = array();
$content = array(); 

Then attempting to sort the results into an associative array where the 'number' is the key and the 'body' is the value.

while ($row = mysqli_fetch_assoc($r)) {
    $array = array(
    $content[$row['Number']] = $row['Body']
);

This works fine except it will not store the first value. This is the result of print_r($content);, missing the first row.

Array ( [2] => This is entry two [3] => This is entry three [4] => This is entry four ) 

I have tried running the SQL query within PHPMyAdmin and it returns all four rows as I would expect.

Does anyone have any ideas what I'm doing wrong?

1 Answer 1

2

$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
You are getting first returned row by this line. You have to remove it and it will work properly.

mysqli_fetch_array and mysqli_fetch_assoc returning NEXT row on every call.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like that got it. Seems incredibly obvious now! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.