I need help. I've got a weird PHP error and it is driving me nuts. I can't figure out what's wrong. This is the first time I've used PHP (I'm just starting) but I can't figure it out. This is PHP 4.3.11, Apache 1.3.33, MySQL 4.1.15 (not involved yet). Here is the code for the two files:
db.php:
index.php:
Now when this is run, the page doesn't finish rendering. Looking at the Apache error log, I get this:
Now line 16 of that file is the 'function disconnectFromDatabase' line. There is no array there. It has also given me this error on line 24 of that file ('$query_string = ...') if I move some of the code (specifically the '$result = new array()' line) around.
I'm going nuts with this. Can anyone point me to the problem???
Edit: Fixed a missing ' that was in my code but got lost when I pasted it here.
db.php:
Code:
<?php
// A function to connect to the database
function connectToDatabase() {
$db = mysql_connect('localhost', 'the_database', 'thePassword')
or die('Could not connect: ' . mysql_error());
mysql_select_db('asc_db')
or die('Cound not select database: ' . mysql_error());
return $db;
}
// A function to disconnect from the database
function disconnectFromDatabase($theDB) {
mysql_close($theDB);
}
// Get a list of privilege levels and their names, returned in an associated array
function getPrivilegeLevels() {
$query_string = 'SELECT privilege_level_id, name FROM privilege_levels';
$query_result = mysql_query($query_string);
if (!$query_result) {
die('Could not retrieve privilege leves from database: ' . mysql_error());
}
$result = new array();
while($row = mysql_fetch_array($query_result, MYSQL_NUM)) {
$result[$row[1]] = $row[0]; // Name value pairs of with name as key and ID as value
}
mysql_free_result($query_result);
return $result;
}
?>
index.php:
Code:
<html>
<head>
<title>Database Test</title>
</head>
<body>
<p>Privilege levels:</p>
<p>
<?php
require 'scripts/db.php';
$db = connectToDatabase();
$level = getPrivilegeLevels();
?>
<table border="1">
<tr>
<th scope="col">Name</th>
<th scope="col">ID</th>
</tr>
<?php
foreach($level as $k => $v) {
echo "<tr><td>$k</td><td>$v</td></tr>";
}
?>
</table>
<?php
disconnectFromDatabase($db);
?>
</p>
</body>
</html>
Now when this is run, the page doesn't finish rendering. Looking at the Apache error log, I get this:
Code:
[error] PHP Parse error: parse error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in /Users/michael/Sites/scripts/db.php on line 16
Now line 16 of that file is the 'function disconnectFromDatabase' line. There is no array there. It has also given me this error on line 24 of that file ('$query_string = ...') if I move some of the code (specifically the '$result = new array()' line) around.
I'm going nuts with this. Can anyone point me to the problem???
Edit: Fixed a missing ' that was in my code but got lost when I pasted it here.