2

I am trying to use php to dynamically fill in a drop down box from a database. at the start my php file I have this before I start my html code.

 <?php 
 include_once "queryfunction.php";
 connect(); 
 ?> 

The addition to this code causes a server error 500. If I comment out those two lines, the website runs (but obviously not the way I want it to). Here is my queryfunction.php code

<?php 



function connect(){


$servername = "localhost";
$username = "root";
$password = "*******";
$database = "lab4"; 
// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
mysql_select_db($database);
}

function close(){ 

mysql_close(); 
}

function query(){ 

$mydata = mysql_query("Select * from Country");
while($record = mysql_fetch_array($mydata)){

echo '<option value = "' . $record['CountryAbbreviation'] . '">' .         $['CountryAbbreviation'] . '"</option>';

}

}

?> 

Yes I've made sure I'm using the right username, password, database, and servername. I'm using amazon instant web service if that helps. I did the actions to turn on displaying errors in php but i still get no info besides server error 500 when the page doesn't run. Thanks guys

  • 4
    `mysqli` doesnt work with `mysql_`. See http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php for more information. Also you should check your error log, it is important to be able to read your errors. – chris85 Mar 08 '16 at 04:00

1 Answers1

1

You missed out record in this line, causing a syntax error:

echo '<option value = "' . $record['CountryAbbreviation'] . '">' .         $['CountryAbbreviation'] . '"</option>';

It should be:

echo '<option value = "' . $record['CountryAbbreviation'] . '">' .         $record['CountryAbbreviation'] . '"</option>';

Also, you cannot mix MySQL functions with MySQLi functions.

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
mysqli_select_db($conn, $database);
}

function close(){ 

mysqli_close(); 
}

function query(){ 

$mydata = mysqli_query($conn, "Select * from Country");
while($record = mysqli_fetch_array($mydata)){

echo '<option value = "' . $record['CountryAbbreviation'] . '">' .         $record['CountryAbbreviation'] . '"</option>';

}
Panda
  • 6,955
  • 6
  • 40
  • 55