user3759862
user3759862

Reputation: 57

Mysql select auto skip first value

i have mysql table looks like this one

id  title       start            resourceId

5   event1  2014-08-15 08:00:00     STK1    
6   event2  2014-08-15 08:00:00     STK2    
7   event3  2014-08-15 08:00:00     STK3    

And php query:

<?php 
include("config.php");

$data = mysql_query ("SELECT * FROM `evenement`");
$zaznam = mysql_fetch_array ($data);
while($zaznam = mysql_fetch_array ($data))
{
   $arr2[] = $zaznam["start"].$zaznam["resourceId"];
}

print_r($arr2);
?>

This query works almost fine, but i always get only event2 and 3. It absolutely won't see first result at all.

Here is my print_r($arr2);

Array ( [0] => 2014-08-15 08:00:00STK2 [1] => 2014-08-15 08:00:00STK3 )

I dont understand why i get only two results. It work all time, but today, I emptied my table and it stop work.

Upvotes: 1

Views: 151

Answers (4)

manitaz
manitaz

Reputation: 1180

Remove this line from your code before the while loop.

$zaznam = mysql_fetch_array ($data);

You get error because when call to mysql_fetch_array function it get the results from the mysql_query results.

Upvotes: 0

Ende Neu
Ende Neu

Reputation: 15773

You are calling mysql_fetch_array twice, remove this line:

$zaznam = mysql_fetch_array ($data);

You are never using it anyway since you overwrite the value in the while loop.

Also note that mysql_* functions are deprecated, use mysqli_* instead.

Upvotes: 1

VMai
VMai

Reputation: 10336

You've got two calls of mysql_fetch_array(), the first one before the loop gets the first row.

Use

$data = mysql_query ("SELECT * FROM `evenement`");
while($zaznam = mysql_fetch_array ($data))
      {
       $arr2[] = $zaznam["start"].$zaznam["resourceId"];
      }

instead.

Upvotes: 2

Shaeldon
Shaeldon

Reputation: 883

You are fetching your resoults twice:

 $zaznam = mysql_fetch_array ($data);
        while($zaznam = mysql_fetch_array ($data))

remove the first one.

Upvotes: 1

Related Questions