StuartBombay
Programmer
I'm trying to build a function to use for creating drop downs. PHP 5, MySQL 5, Apache2.
The configuration: configdb.inc
The connection: opendb.inc
I get $conn not defined when I try to run this, I don't understand because it is passing the database name?
This include method works when I run a query and display a table - but this does not work.
Can anyone explain to me what's wrong?
The code is incomplete, I've take a lot out in an attempt to debug.
Thanks.
The configuration: configdb.inc
Code:
<?php
// Configure database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'sitedata';
?>
The connection: opendb.inc
Code:
<?php
// Open the database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to mysql '.mysqli_error());
mysqli_select_db($conn, $dbname);
?>
Code:
<?php
require('../../includes/configdb.inc');
require('../../includes/opendb.inc');
echo "From opendb.inc: $dbname";
function drop_down($intID, $strName, $tableName, $strOrderField, $strMethod="asc") {
$strQuery = "select $intID, $strName from $tableName order by $strOrderField $strMethod";
echo "<p> $strQuery </p>";
$result = mysqli_query($conn, $strQuery);
while($arrayRow = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$intIdField = $arrayRow["$intID"];
$strNameField = $arrayRow["$strName"];
echo "<option value=\"$intIdField\">$strNameField</option>\n";
}
echo "</select>\n\n";
}
drop_down('prognum', 'fullname', 'programs', 'lastname');
?>
Can anyone explain to me what's wrong?
The code is incomplete, I've take a lot out in an attempt to debug.
Thanks.