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];
3

9 Answers 9

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];
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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

Comments

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

Comments

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.

Comments

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

Comments

0

Nice and clean version:

while($row = mysql_fetch_array($sqlquery))

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

}


echo $items[0];
echo $items[1];

Comments

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];
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.