0

I am trying to fetch the data from table and store it in the array named "items" and then access it using index. I am getting an error "Undefined offset: 0" . What is wrong in the code ?

$i=0;

while($row = mysql_fetch_array($sqlquery))
{
    $name = $row['name'];

    $items = array();

    $items[$i] = $name;

    $i= $i +1;
}

echo $items[0];
echo $items[1];
juco
  • 6,331
  • 3
  • 25
  • 42
Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • define the i value `$i=0;` – iLaYa ツ Mar 22 '13 at 12:04
  • check this question: http://stackoverflow.com/questions/4455548/storing-values-from-mysql-table-into-an-array-in-php – danywalls Mar 22 '13 at 12:05
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Brock Hensley Mar 22 '13 at 12:05

9 Answers9

5

Define $items outside of your loop, define $i = 0 outside your loop and change to mysql_fetch_assoc and try that.

$i = 0;
$items = array();
while($row = mysql_fetch_assoc($sqlquery)) {
    $name = $row['name'];

    $items[$i] = $name;
    $i++;
}
echo $items[0];
echo $items[1];
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
4

You're recreating array on every iteration. Initialization of result array ($items in your code) should be outside of loop. E.g.:

$items = array();

while($row = mysql_fetch_array($sqlquery, MYSQL_ASSOC))
{

    $name = $row['name'];

    $items[] = $name;
}
echo $items[0];

echo $items[1];

Note: mysql_* functions are deprecated, use mysqli_* or PDO.

Leri
  • 12,367
  • 7
  • 43
  • 60
2

You are re-declaring your array each time inside loop which is incorrect.

$items = array();
while($row = mysql_fetch_assoc($sqlquery))
{
  $items[] = $name = $row['name'];
}
echo $items[0];
echo $items[1];

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
0
  1. Add $i = 0; before while loop
  2. Add $items = array(); before while loop, Remove it from inside while loop

     $i         = 0;
     $items = array();
     while($row = mysql_fetch_assoc($sqlquery))
    
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

use this

while($row = mysql_fetch_array($sqlquery))

{

$name = $row['name'];

$items = array();

$items[] = $name;




}
if(isset($items[0])){

echo $items[0];
}

if(isset($items[1])){
echo $items[1];
}

No need of using $i

user7282
  • 5,106
  • 9
  • 41
  • 72
0

Move $items = array() out of the while loop. It's been redefined multiple times and the $item[0] is lost. You only get the last item in the resulting array.

Antony
  • 14,900
  • 10
  • 46
  • 74
0

You shouldn't declare your variables inside the loop and if you're just adding to a single dimensional array you can leave the $i off.

$items = array();

while($row = mysql_fetch_assoc($sqlquery))
{
    $items[] = $row['name'];
}

print_r($items);
Cameron Chapman
  • 796
  • 9
  • 19
0

Nice and clean version:

while($row = mysql_fetch_array($sqlquery))

{
   $items[] = $row['name'];

}


echo $items[0];
echo $items[1];
unicorn80
  • 1,107
  • 2
  • 9
  • 15
0

You are initializing your array each time in the loop.

Do like that:

<?php
$items = array();
while($row = mysql_fetch_array($sqlquery)) {
    $name = $row['name'];
    $items[] = $name;
}
echo $items[0];
echo $items[1];
?>
Harpreet
  • 709
  • 5
  • 14