1

I have an index.php which calls the DB connection and populates the page. If a condition is met an include will be called upon with a dropdown list. I am unable to populate this dropdown, it gives me no error just a blank screen.

index.php:

<?php
require_once('db_connection.php');
if($_GET["cat"] === "contact"){
        include ('includes/contact.php');
    }
    else{
        include ('includes/route.php');
    }
?>

contact.php:

<?php
$sql = "SELECT * FROM dropdown_town";
$result = $dbhandle->query($sql);
$town = $result->fetch_assoc();

while ($row = mysql_fetch_array($town)) {
 echo "<option value='" . $row['zip'] . "'>" . $row['townname'] . " </option>";
 }
?>

My DB has a table called dropdown_town with the columns ID, zip and townname I only posted the PHP code here as the rest of the page runs ok

  • Did you get the result from your query. if not checked then just do print_r($town) after fetching record; – Priyank Jul 29 '19 at 09:33
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Dharman Jul 29 '19 at 19:32

1 Answers1

1

Do the following changes in your contact.php

You forgot yo write select and directly write option because of that you got the blank page.

<?php
$sql = "SELECT * FROM dropdown_town";
$result = $dbhandle->query($sql);
echo "<select name="selectboxname">";
while ($row = mysql_fetch_array($result)) {
     echo "<option value='" . $row['zip'] . "'>" . $row['townname'] . " </option>";
 }
 echo "</select>";
?>
Priyank
  • 470
  • 2
  • 11
  • The select was written earlier as I wanted a blank option in the form: Code: ' ' – Paul Zelluf Jul 29 '19 at 09:47
  • If I remove the While and only use the echo line where I change $row to $result I get 1 option, the first from the DB. It seems that the while routine doesn't like to run for some reason – Paul Zelluf Jul 29 '19 at 09:54
  • No need to remove while loop and give me your new changes so that i can check. – Priyank Jul 29 '19 at 09:57
  • did you get options? As i have seen you have added LIMIT 1 in your query. this will fetch only one record from the database – Priyank Jul 29 '19 at 10:10
  • Removed the Limit 1 and the code below works (only 1 option, since I comment out the while, when I uncomment it, blank screen again :( – Paul Zelluf Jul 29 '19 at 10:14
  • No, it doesnt work, the while loop gives a blank screen :( btw: how to comment the code nicely? – Paul Zelluf Jul 29 '19 at 10:19
  • what you get in $town, just do print_r($town); then check and let me know what you will get in my $town – Priyank Jul 29 '19 at 10:27
  • The following worked :) $sql = mysqli_query($dbhandle, "SELECT * FROM dropdown_town ORDER BY zip"); while ($row = $sql->fetch_assoc()){ echo ""; } Kind regards – Paul Zelluf Jul 29 '19 at 14:19
  • Found that out :) – Paul Zelluf Jul 30 '19 at 11:10