0

hi using following php code I am trying to populate a dropbox from mysql database:

enter code here
<?php
$mysqli = new mysqli('localhost', 'root', '', 'testdb');
if (mysqli_connect_errno())
 {
   die('Unable to connect!');
 }
else
{
 $query = 'SELECT * FROM language';
  if ($result = $mysqli->query($query))
   {
    if ($result->num_rows > 0)
     {
 ?>
   <p> Select a language
    <select id="selectLanguage">
    <option value="">select</option>
 <?php
    while($row = $result->fetch_assoc())
 {
  ?>
  <option value='<?php echo $row[0]; ?>'><?php echo $row[1]; ?></option>
 <?php
  }
   ?>
 </select>
 </p>
  <p id="result"></p>
 <?php
  }
 else
  {
   echo 'No records found!';
   }
 $result->close();
   }
  else
   {
      echo 'Error in query: $query. '.$mysqli->error;
    }
   }
   $mysqli->close();
   ?>

I am not getting any error but the dropbox populated by this message instead of cell values stored in database: (!) Notice : Undefined offset:0 in C:\wamp\test\index.ph on line 40 Call Stack#TimeMemoryFunctionLocation 10.0009684656{main}()...\index.php:0'> can you please let me know why this is happening?

1 Answer 1

2

You problem is here:

<option value='<?php echo $row[0]; ?>'><?php echo $row[1]; ?></option>

you fetch the data using an associated array. not an index array.

 while($row = $result->fetch_assoc())

You get that error message because index "1" does not exist.

4
  • Thansk jaechuleta, but I have two column in my table! id and name so I have the index 1, am i right? if not can you please let me know how I can fix the error? Commented Nov 7, 2012 at 0:32
  • what does print_r($row); exit(); output? put it right after the while.
    – jarchuleta
    Commented Nov 7, 2012 at 0:33
  • Ok I did like this <?php while($row = $result->fetch_assoc()) print_r($row); exit(); { ?> and it just print out the empty dropdown without any error message Commented Nov 7, 2012 at 0:36
  • when you do the print $row view the source and look at the output, then paste that here.
    – jarchuleta
    Commented Nov 7, 2012 at 15:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.