1

I've seen at least a few duplicate questions regarding this already but for some reason I can't get the data to show up in the menu. The drop down menu is just blank.

The table is "Colleges2". The data I am trying to fetch is in a column called "Name". So it basically fetches and displays a list of Names from the Colleges2 table.

mysql_connect('localhost', '', '') or die(mysqli_error()) ;
mysql_select_db('Colleges2');

$sql = "SELECT Name FROM Colleges2";
$result = mysql_query($sql);

?>
<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
$Name = $row['Name'];
echo "<option value='" .$row['Name']. "'>" .$row['Name']. "</option>";
} ?>
</select> 

I am probably not using the latest mysqli code....forgive me for that. I keep seeing posts and tutorials that use either mysql or mysqli.

UPDATED:

<?php
$sql = "SELECT Name FROM Colleges2";
$conn = new mysqli("localhost", "", "") or die("Failure!") ;

$stmt=$conn->query($sql);

?>
<select name="Name" id="">
<?php
while ($row = $stmt->fetch_assoc() ) {
$Name = $row['Name'];
echo "<option value='" .$Name. "'>" .$Name. "</option>";
} ?>
</select> 

SECOND UPDATE (THIS ONE WORKED):

<?php
//$host = "localhost:3306";
//$db_name="univers1_test";
//$user = "univers1_admin";
//$pass = "B@ctad89";

 //$conn = new mysqli($host, $user, $pass, $db_name) or die("DB Connection failed!!");
mysql_connect("localhost:3306", "", "") or die(mysql_error()) ;
mysql_select_db("univers1_test") or die(mysql_error()) ;
$sql = "SELECT Name FROM Colleges2";
$result = mysql_query($sql)  or die(mysqli_error()) ;

//$stmt=$conn->query($sql);
?>

<select name="Name" id="">
<?php
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php $row['Name']; ?>"><?php echo $row['Name']; ?>     </option>
<?php
} ?>
</select> 
user2984757
  • 29
  • 3
  • 8
  • 1
    [**Can I mix MySQL APIs in PHP?**](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) _Spoiler alert:_ no, you **can't**. – FirstOne Jul 03 '16 at 15:31
  • You did a great job describing what you already have. But we don't know the exact problem you are facing. Can you enlight us? – PaulH Jul 03 '16 at 15:31
  • In case you didn't see it, `mysqli_error` is a mix – PaulH Jul 03 '16 at 15:33
  • Things to consider: 1- you assign `$row['Name']` to `$Name` but never uses it. 2- Inspect the page and check the generated source. 3- [mysql_error](http://php.net/manual/en/function.mysql-error.php) after query. 4 - Are there any rows inserted in that table? – FirstOne Jul 03 '16 at 15:36
  • 1
    [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – FirstOne Jul 03 '16 at 15:38
  • if you have phpmyadmin, try your SQL statement there. Also, check the return value of `mysql_query()` – PaulH Jul 03 '16 at 15:38
  • @PaulH - the data from the database table is not showing. I'm trying to fetch and display them. – user2984757 Jul 03 '16 at 15:40
  • SELECT Name FROM Colleges2 <-- when run in phpmyadmin, I get all of the values from the Name column in the Colleges2 table. – user2984757 Jul 03 '16 at 15:43
  • It is clear what the end result must be, thanks. You should debug at intermediate steps and see if the results are correct. `Name` could be `name` for instance and your configuration could be case sensitive. – PaulH Jul 03 '16 at 15:44
  • So your SQL statement works. Nice. What do you see in HTML? – PaulH Jul 03 '16 at 16:47
  • 1
    Nice update to mysqli – PaulH Jul 03 '16 at 18:40

2 Answers2

1

Here's how to use PDO or MySQLi

PDO

<?php
$host = "localhost";
$db_name="Colleges2";
$user = "";
$pass = "";

$sql = "SELECT Name FROM Colleges2"; 
$conn = new PDO("mysql:host=$host;dbname=$db_name",$user,$pass) or die("DB Connection failed!!");
$stmt   =   $conn->prepare($sql);
$stmt->execute();
?>
<select name="Name" id="">
<?php
while ( $row   =   $stmt->fetch(PDO::FETCH_ASSOC) ) {
$Name = $row['Name'];
echo "<option value='" .$Name. "'>" .$Name. "</option>";
} ?>
</select> 

MySQLi

<?php
$host = "localhost";
$db_name="Colleges2";
$user = "";
$pass = "";

$sql = "SELECT Name FROM Colleges2"; 
$conn = new mysqli($host, $user, $pass, $db_name) or die("DB Connection failed!!");
$stmt=$conn->query($sql);
?>
<select name="Name" id="">
<?php
while ( $row   =   $stmt->fetch_assoc() ) {
$Name = $row['Name'];
echo "<option value='" .$Name. "'>" .$Name. "</option>";
} ?>
</select> 
PaulH
  • 2,918
  • 2
  • 15
  • 31
  • Used the mysqli version, but the data is still not being displayed in the drop menu... :( – user2984757 Jul 03 '16 at 15:58
  • @user2984757 In that case, please check your database. Make sure the database name and table is **Colleges2**, and that the name of the column is **Name** – Clyde Scope Jul 03 '16 at 16:03
0
<?php
  mysql_connect('localhost', '', '') or die(mysqli_error()) ;
  mysql_select_db('Colleges2');

  $sql = "SELECT Name FROM Colleges2";
  $result = mysql_query($sql) or die(mysqli_error()) ;
?>

<select name="Name" id="">
<?php
  while ($row = mysql_fetch_array($result)) {
?>
 <option value="<?php echo $row['Name']; ?>"> <?php echo $row['Name']; ?> </option>
 <?php
 } ?>
</select> 
Ravi Roshan
  • 570
  • 3
  • 14
  • 1
    please explain what you did – PaulH Jul 03 '16 at 18:21
  • I checked the differences, and don't see why it would help. Isn't it applying an other coding style, but not changing behaviour? – PaulH Jul 03 '16 at 18:47
  • I tried mysqli but for some reason, when I tried this code, it worked. Data is being displayed now. Updated my post with the solution. Do you guys have any feedback? I never know why mysql can work better for me than mysqli. – user2984757 Jul 04 '16 at 00:15