开发者

PHP dropdown with items from database

I can't seem to get values of a table in my dropdownlist. I get no errors, only an empty dropdownlist. This is my code:

<?php               
include_once ("classes/Keten.class.php");
$keten = new Keten();
$allKet = $keten->getAllKetens();

echo '<select name="fk_keten_id">';             
while($row = mysql_fetch_array($allKet))
{
  echo '<开发者_如何学编程;option value="' . 
       $row['pk_keten_id'] . '">' . $row['keten_naam'] . '</option>';
}
echo '</select>'; 
?>

Function getAllKetens works, because I use it in an other page.

Thanks in advance :)

on request, the getAllKet() function in classes/Keten.class.php

public function getAllKetens()
    {
        include ("connection.php");
        $sSql = "SELECT * FROM tblKetens;";
        $vResult = mysqli_query($link, $sSql);
        mysqli_close($link);
        return($vResult);
    }


I'm not sure mixing mysqli and mysql commands can be done, in your fetch_array you used mysql_fetch_array instead of mysqli_fetch_array.


Your function getAllKetens() is closing the database. You shouldn't be trying to fetch a row after closing the connection to the database.

Soap box aside: It might be nice to have some more standard naming convention going on in there, as well. If you're returning a handle to a mysql query object, consider $query or $hQuery instead of what's (to me) a lot more cryptic as $allKet. IMO, if a function is called "getAllKetens()" it should return an array of data, not a handle to a query.


  • Make sure errors are turned on
  • View the source, If there are any errors, they will be hidden by the select tag
  • Do a print_r($allket); to see if getAllKeten() actually returned anything.


I suspect the $allKet is not what you think it is.

First, check the HTML. If you are getting <select name="???"></select> then

a) Check $allKet is a resouce (I think print_r will tell you). b) Check that the SQL is selecting rows. (try it in phpmyadmin) c) Check that you have correct spellings for field names.

If you are seeing <option value="??"></option> entries, then

a) Check the field names you are printing are correct b) Check you are not hiding them with an alias (the print will show the last field).

Turn error reporting up (in php.inf) and remove any errors that are reported.


This part of your code looks fine:

echo '<select name="fk_keten_id">';             
while($row = mysql_fetch_array($allKet))
{
  echo '<option value="' . 
       $row['pk_keten_id'] . '">' . $row['keten_naam'] . '</option>';
}
echo '</select>';

Your problem should lie in $allKet.

Do you need to give Keten() any arguments to initialize anything?


Ok I got it

use: while($row = mysqli_fetch_assoc($allKet))

instead of: while($row = mysql_fetch_array($allKet))

Thank you all for your fast replies !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜