Page 1 of 1

Can't figure out what is wrong with this script

Posted: Sun Jan 31, 2010 2:10 am
by Jackolantern
This is a modified script from Halls' tutorial. It is after my login script, and I am trying to pull news out of my news table to display for the player before they log into the game. Everything was going great until I got a parse error. Here is the script:

Code: Select all

<?php
include 'connect.php';
session_start();

////Get shorter variable names
$userName = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);

////Query db for username and password for comparison
$query = "SELECT * from players where name='$userName'";
$qResults = mysqli_query($db, $query) or die("Could not query db to get player info.");
$qArray = mysqli_fetch_assoc($qResults);

////See if any results were found.
if (!$qArray) {
	echo "No player found. Please try again.<br />";
	echo "<a href='login.php'>Try again.</a>";
	exit;
}

////See if the password is incorrect
if ($password != $qArray['password']) {
	echo "Password incorrect. Please try again. <br />";
	echo "<a href='login.php'>Try again.</a>";
	exit;
}

////Check to see if a playername is already being stored in $_SESSION, perhaps
////if the player used their browser to come back to this page. If there is 
////no session name stored, create one
if (!isset($_SESSION['cPlayer'])) {
	$_SESSION['cPlayer'] = $userName;
}

////Welcome the player, and retrieve news from the 'news' table
echo "Welcome ".$_SESSION['cPlayer']."!<br />";
?>
<br /> 
<b><u>News:</u></b>

<?php 

$nQuery = "SELECT * from news WHERE newsid=(select MAX(newsid) from news)";
$nResult = mysqli_query($db, $nQuery);
$nArray = mysqli_fetch_assoc($nResult);

////Make sure news was found and fail gracefully if it was not
if (!isset($nArray['text'])) {
	echo "No news found. Please check back for more news later. <br />";
	echo "<p>Enter the game!</p><br />";
	echo "<a href='mapscreen.php'>Enter</a>";
	exit;
}

////Assume news was found, and display for the user
if (isset($nArray['text'])) {
	echo "$nArray['text']";
}

////Display the link for the player to 
////enter the game
echo "<br />";
echo "<p>Enter the game!</p><br />";
echo "<a href='mapscreen.php'>Enter</a>";


?>
The query is supposed to be getting the news story with the highest "newsid", which is an auto_increment primary key field (thus grabbing the most recent news post without having to fuss with dates for now). I am getting this parse error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\blastedsky\loginproc.php on line 66
Here is line 66:

Code: Select all

////Make sure news was found and fail gracefully if it was not
if (!isset($nArray['text'])) {
	echo "No news found. Please check back for more news later. <br />";
	echo "<p>Enter the game!</p><br />";
	echo "<a href='mapscreen.php'>Enter</a>";
	exit;
}

////Assume news was found, and display for the user
if (isset($nArray['text'])) {
	echo "$nArray['text']"; //<<<<<<<<<<<<<<<<<-----Line 66
}

////Display the link for the player to 
////enter the game
echo "<br />";
echo "<p>Enter the game!</p><br />";
echo "<a href='mapscreen.php'>Enter</a>";
(Added some code above and below, since I know it is not always the exact line on parse errors.)

I just can't figure this one out. I have already used the $nArray['text'] value in a conditional in the lines above it and I don't think it was a problem there. So what is the problem with displaying it?

Re: Can't figure out what is wrong with this script

Posted: Sun Jan 31, 2010 2:48 am
by OldRod
I don't think you need the ' around text when it's inside an echo statement.

I've hit a similar problem before and it's very confusing.

Other than that, I don't see anything wrong...

Re: Can't figure out what is wrong with this script

Posted: Sun Jan 31, 2010 3:43 am
by Jackolantern
OldRod wrote:I don't think you need the ' around text when it's inside an echo statement.

I've hit a similar problem before and it's very confusing.

Other than that, I don't see anything wrong...
That was it! Thank you very much :) That is awfully strange, though, I agree lol

Re: Can't figure out what is wrong with this script

Posted: Sun Jan 31, 2010 10:23 am
by Chris
3 possibilities here :P

1. echo "{$nArray['text']}";
2. echo $nArray['text'];
3. echo "$nArray[text]";

I'd go with possibility 1.