0

I am trying to store the id from the database into an array using PHP but it shows an error.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webmirchi";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id FROM jobinfo";
$result = $conn->query($sql);
$data = array();
while($row = $result->fetch_row())
{
$data[] = $row['id'];
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
$arrlength = count($data);
for($x = 0; $x < $arrlength; $x++) {

echo $data[$x];
echo "<br>";
}
$conn->close();
?>

Notice: Undefined index: id in E:\xampp\htdocs\demo1.php on line 19

1 Answers1

0

You have to target the index not column name 'id'. So this will work:

$sql = "SELECT id FROM jobinfo order by 1 asc";

if ($result = $conn->query($sql)) {
   $data = array();

   while($row = $result->fetch_row())
   {
      $data[] = $row[0];
   }

   $arrlength = count($data);
   for($x = 0; $x < $arrlength; $x++) {
      echo $data[$x];
      echo "<br>";
   }
}
O.S.Kaya
  • 108
  • 2
  • 8