MrSlippyFist
MrSlippyFist

Reputation: 501

Why does my array start with empty value?

I can't wrap my head around this one.

When I create an array it begins with an empty value. The values that needs to be in the array starts as second.

Array:

Array ( 

 [0] => 

 [1] => Value 1 

 [2] => Value 2 

 [3] => Value 3

)

Code:

$categories = array();
$query2 = mysql_query("SELECT * FROM books_categories");
do{
    array_push($categories, $category['description']);
}while($category=mysql_fetch_assoc($query2));

How do I accomplish to get the first item in the array to be Value 1?

Upvotes: 12

Views: 599

Answers (6)

senK
senK

Reputation: 2802

It is because of do while loop try while loop

while($category=mysql_fetch_assoc($query2)){
    array_push($categories, $category['description']);
}

Upvotes: 21

NullPoiиteя
NullPoiиteя

Reputation: 57322

How do while loop works

enter image description here

The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).source

so in your program what happened you are pushing the first element before you read the first row.

so use while instead

what wile loop will do ? as in flow chart it will check first than come inside loop body so element is push after read the first row

$categories = array();
$query2 = mysql_query("SELECT * FROM books_categories");

while($category=mysql_fetch_assoc($query2)) {
    array_push($categories, $category['description']);
}

Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Upvotes: 12

Jonas m
Jonas m

Reputation: 2734

Try doing this instead:

$categories = array();
$query2 = mysql_query('SELECT * FROM books_categories');
while($r = mysql_fetch_array($query2)){
    $categories[] = $r['description'];
}

Upvotes: 1

Veger
Veger

Reputation: 37905

You first write the value (array_push()) into the array and then you actually grab the value (mysql_fetch_assoc()).

You need to first grab the value and then place it into your array!

$categories = array();
$query2 = mysql_query("SELECT * FROM books_categories");
while(($category = mysql_fetch_assoc($query2)) !=== false) {
    array_push($categories, $category['description']);
}

Upvotes: 5

Aleks G
Aleks G

Reputation: 57316

The answer is very simple: you are pushing the first element before you read the first row. The right way of doing it would be with a pre-condition loop:

$categories = array();
$query2 = mysql_query("SELECT * FROM books_categories");

while($category=mysql_fetch_assoc($query2)) {
    array_push($categories, $category['description']);
}

And, please switch to using PDO.

Upvotes: 14

Shoe
Shoe

Reputation: 76240

Because you should use a while loop instead of the do-while loop.

while($category = mysql_fetch_assoc($query2)) {
    array_push($categories, $category['description']);
}

In your code instead in the first iteration of the loop (when entering the do block for the first time), $category is not set therefore setting the first array value to be empty.

Upvotes: 5

Related Questions