• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Weird PHP error driving me nuts!

mbcook

Limp Gawd
Joined
Jul 27, 2001
Messages
438
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:

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.
 
Code:
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;
}

Not sure if the code you entered was copy/pasted, but you were missing a ' after thePassword. The error you posted is usually due to a missing quote, brace, semicolon or something along those lines.

Also, the die error message has a spelling error, not that it is causing any problems, just something I noticed.
 
It was copy/pasted, but I changed the user and password for obvious reasons and accidently deleted that tick mark then. It's there in my real code.

I've done other programming, so I've been looking for missing things (semicolons, parentheseis, etc) but I can't find any.

The odd thing is how the error occurs. For example, let's take the line that says $query_string="...."; That was where the error first showed up, then it "moved" during testing.

If I copy and paste that line and put it one above the other, the first line is "ok" but the second gives the error. If I split the line up into many different lines each with a different word (each line doing $query_string += "....") the the last line is always the one that fails. If I comment that line out, the line above that now becomes the error.

I can't figure this out and it is driving me nuts.
 
Could someone just make the two files and run them on their machine to see if they will run (even without the database, I'll find out if you get the same parse error as me). I don't have access to any other machines with PHP to test them on.
 
I just created the files and ran it in my browser, similar output:

Code:
Privilege levels:


Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'$'' in C:\Program Files\Apache Group\Apache2\htdocs\scripts\db.php on line 31

I will now conduct an investigatio

never fear, maybe I'll find something
 
well I check line 31 like my error said:

Code:
  $result = new array();

probably should be:
Code:
  $result = array();


php may be based on C and higher level languages, but you basically just write crap whatever and it usually comes out ok
 
That did it. Thanks.

I'm well aware of the "just try stuff until it works" part, but none of my trying had worked yet. Based on that error message I've been looking for missing stuff, using reserved words, etc. And since the error was often told to be BEFORE that line of the code, I hadn't even looked at that.

I'm still getting my head around PHP though. It is somewhere between a cross of a half C/C++ language with Perl.

I wonder if the PHP5 parser is any better. Too bad I can only use PHP4

I can't thank you enough for your help. I've been pulling my hair out all day over that.
 
Back
Top