Parse error: syntax error, unexpected $end in...  | Member | | Join Date: Jun 2009 Location: Indiana
Posts: 65
| |
I keep getting this error and I cannot figure it out. My curly brackets are closed, and I am using the correct tags for <?php to open and ?> to close my code. Can someone please help me!
Here is my code: -
<?php
-
//Connect To Database
-
$hostname='mideasthonors2.db.4381361.hostedresource.com';
-
$username='**************';
-
$password='***********';
-
$dbname='mideasthonors2';
-
$usertable='admin_tasks';
-
$yourfield = 'Name';
-
-
mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
-
mysql_select_db($dbname);
-
-
$query = 'SELECT * FROM $usertable';
-
$result = mysql_query($query);
-
if($result) {
-
while($row = mysql_fetch_array($result)){
-
$name = $row['$yourfield'];
-
echo 'Name: '.$name;
-
-
mysql_select_db("mideasthonors2");
-
-
$sql="INSERT INTO $usertable (Name, Address 1, Address 2, City, State, Zip Code, Website, Contact Name, Position, Phone Number, Email Address)
-
-
VALUES
-
-
{$_POST['name']},{$_POST['address 1']},{$_POST['address 2']},{$_POST['city']},{$_POST['state']},{$_POST['zip code']},{$_POST['website']},{$_POST['contact name']},{$_POST['position']},{$_POST['phone number']},{$_POST['email address']};
-
-
if (!mysql_query($sql,$con))
-
-
{
-
-
die('Error: ' . mysql_error());
-
-
}
-
-
echo '1 record added';
-
-
mysql_close($con);
-
}
-
}
-
?>
| | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: Parse error: syntax error, unexpected $end in...
There must be some trailing spaces or output before. Make sure you dont have a blank line at the top ie:
It has to be: You cannot have any spaces or any output before you use a header() call.
Please paste the entire contents of your code. ALso admin-dbcon.php but remove any sensitive information like passwords, etc.
| | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: Parse error: syntax error, unexpected $end in...
In fact attach the files so we can see where the output is being generated.
|  | Member | | Join Date: Jun 2009 Location: Indiana
Posts: 65
| | | re: Parse error: syntax error, unexpected $end in...
Code from adminloginprocess.php: -
<?php
-
include("admin-dbcon.php");
-
-
// you need this to be able to use sessions
-
session_start();
-
-
//sanitize the user input to ensure random usernames cannot be used
-
$sUsername= mysql_real_escape_string($_POST['Username']);
-
$sPassword = mysql_real_escape_string($_POST['Password']);
-
-
//ensure username and password are valide
-
$qry = mysql_query("
-
SELECT * from admin_login
-
WHERE = username = '$sUsername'
-
AND password = '$sPassword'
-
");
-
-
// check the number of rows returned
-
if(mysql_num_rows($qry) == 1)
-
{
-
// not a valid login redirect to fail
-
header("Location: login-error.php");
-
exit;
-
}
-
-
// if you get here then you have made a match
-
// store data in session variables
-
$row = mysql_fetch_array($qry);
-
$_SESSION['username'] = $row['username'];
-
$_SESSION['userid'] = $row['ID'];
-
-
// redirect user onto admintasks.php
-
header("Location: admintasks.php");
-
?>
-
**No spaces are shown
admin-dbcon.php: -
<?php
-
$dbHost = 'mideasthonors.db.4381361.hostedresource.com';
-
$dbName = 'xxx';
-
$dbUser = 'xxx;
-
$dbPass = 'xxx';
-
-
mysql_connect("$dbHost","$dbUser","$dbPass") or die("Error: Unable to connect to database! Please try again later.");
-
-
//Select the database we want to use
-
mysql_select_db($dbName) or die("Error: Could not select database");
-
?>
-
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | re: Parse error: syntax error, unexpected $end in...
make sure that "admin-dbcon.php" does not have any echo or print statements in it. Then delete the closing php bracket (the ?> at the bottom.
if this file does need to output something, then session_start() should come before that include line.
Dan
| | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: Parse error: syntax error, unexpected $end in...
Make sure there are no spaces at the bottom. Seems like you have an extra line underneath where you close PHP ie. ?> on both scripts.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,753
| | | re: Parse error: syntax error, unexpected $end in...
If you can't spot where the output is being sent, try adding ob_start() at the very top of your script (before any includes), and ob_end_flush() at the very bottom (or at least after you header calls).
Those should buffer any output until you are ready to send it.
You can use them to narrow down exactly where the problem is.
|  | Member | | Join Date: Jun 2009 Location: Indiana
Posts: 65
| | | re: Parse error: syntax error, unexpected $end in...
Thank you all for your suggestions. I have applied all of them and here is my code for adminloginprocess.php: -
<?php
-
ob_start()
-
include("admin-dbcon.php");
-
-
// you need this to be able to use sessions
-
session_start();
-
-
//sanitize the user input to ensure random usernames cannot be used
-
$sUsername= mysql_real_escape_string($_POST['Username']);
-
$sPassword = mysql_real_escape_string($_POST['Password']);
-
-
//ensure username and password are valide
-
$qry = mysql_query("
-
SELECT * from admin_login
-
WHERE = username = '$sUsername'
-
AND password = '$sPassword'
-
");
-
-
// check the number of rows returned
-
if(mysql_num_rows($qry) == 0)
-
{
-
-
// not a valid login redirect to fail
-
header("Location: login-error.php");
-
exit;
-
}
-
-
// if you get here then you have made a match
-
// store data in session variables
-
$row = mysql_fetch_array($qry);
-
$_SESSION['username'] = $row['username'];
-
$_SESSION['userid'] = $row['ID'];
-
// redirect user onto admintasks.php
-
header("Location: admintasks.php");
-
ob_end_flush()
-
?>
-
And my code for admin-db.com.php: -
<?php
-
$dbHost = 'mideasthonors.db.4381361.hostedresource.com';
-
$dbName = 'xxx';
-
$dbUser = 'xxx';
-
$dbPass = 'xxx';
-
-
mysql_connect("$dbHost","$dbUser","$dbPass") or die("Error: Unable to connect to database! Please try again later.");
-
-
//Select the database we want to use
-
mysql_select_db($dbName) or die("Error: Could not select database");
-
***Although it appears on here that I have spaces after my last line...it is not true. My ?> on adminloginprocess.php is the very LAST line of my scripting, no other lines appear after it. I am using SharePoint Designer, not sure if that has anything to do with it.
After all of the suggestions from yesterday this is now the error I am receiving:
Parse error: syntax error, unexpected T_INCLUDE in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 3
which has to do with the db-con.php file. However one can see that the db-con.php file has been set up properly, except that I deleted the ?> as suggested by one of you yesterday. I am assuming that if I replaced that I would still be taken to a blank page. I am not really sure what steps to follow next:(
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: Parse error: syntax error, unexpected $end in... Quote:
Originally Posted by mideastgirl After all of the suggestions from yesterday this is now the error I am receiving:
Parse error: syntax error, unexpected T_INCLUDE in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 3 I remember vaguely saying something about unexpected T_*** errors... Quote:
Originally Posted by Dormilich generally, if the error message says something about unexpected T_*** then you should look for missing parentheses, brackets, commas, semi-colons and the like. | | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: Parse error: syntax error, unexpected $end in...
Your missing semi-colons after:
ob_start() and ob_end_flush()
Make sure they are: - ob_end_flush(); and ob_start();
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: Parse error: syntax error, unexpected $end in...
@hoopy: cheater ;)
.......
|  | Member | | Join Date: Jun 2009 Location: Indiana
Posts: 65
| | | re: Parse error: syntax error, unexpected $end in...
Sorry guys,
but that is not the problem. I added the semi-colons and I am still getting this error:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 6
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 20
Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 24
This is my code: -
<?php
-
ob_start();
-
include("admin-dbcon.php");
-
-
// you need this to be able to use sessions
-
session_start();
-
-
//sanitize the user input to ensure random usernames cannot be used
-
$sUsername= mysql_real_escape_string($_POST['Username']);
-
$sPassword = mysql_real_escape_string($_POST['Password']);
-
-
//ensure username and password are valide
-
$qry = mysql_query("
-
SELECT * from admin_login
-
WHERE = username = '$sUsername'
-
AND password = '$sPassword'
-
");
-
-
// check the number of rows returned
-
if(mysql_num_rows($qry) == 0)
-
{
-
-
// not a valid login redirect to fail
-
header("Location: login-error.php");
-
exit;
-
}
-
-
// if you get here then you have made a match
-
// store data in session variables
-
$row = mysql_fetch_array($qry);
-
$_SESSION['username'] = $row['username'];
-
$_SESSION['userid'] = $row['ID'];
-
// redirect user onto admintasks.php
-
header("Location: admintasks.php");
-
ob_end_flush();
-
?>
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: Parse error: syntax error, unexpected $end in... Quote:
Originally Posted by mideastgirl Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 20 mysql_query() returns false if there was an error, which then obviously is not a resource.
remember: always test values that can have different data types and where the data type matters in the next usage.
refer also to mysql_query().
typical example (very basic): - $qry = mysql_query($sql) or die(mysql_error());
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,753
| | | re: Parse error: syntax error, unexpected $end in... Quote:
Originally Posted by mideastgirl Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/adminloginprocess.php:1) in /home/content/m/i/d/mideasthonors/html/adminloginprocess.php on line 6 Since you now have ob_start() on your second line, before the include, it is clear that the output that is preventing you from setting the headers is being added before the <?php on line #1.
There simply is no other explanation for this error. There is something being sent before the <?php on line #1. To fix this, you need to find out what it is and remove it.
A white-space, some hidden unicode marker... something.
P.S.
SharePoint Designer... isn't that just FrontPage for Vista? Does it even support PHP? (Meaning, color highlighting, syntax error highlighter, and such.)
In any case, if you are having a hard time finding the problem in that, try opening your file in Notepad++. It's very good at spotting trash characters and such.
|  | Member | | Join Date: Jun 2009 Location: Indiana
Posts: 65
| | | re: Parse error: syntax error, unexpected $end in...
Atli,
I have opened the document in Notepad ++ and some of the characters are in color, like semi-colons and such. However I do not know how to identify which characters are trash. Are the one's in color trash? Not really sure. I am going to mess with it more and hopefully figure it out.
Dormilich,
Are you suggesting, as is the link you sent me, that I need to use mysql_query() to see if the resource (variable/password?) I am using is a variable at all? Little confused on what that means. I kind of gathered that I need to go into my database and ensure that the password is entered as VARCHAR as opposed to anything else???? Let me know if I am on the right track.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: Parse error: syntax error, unexpected $end in... Quote:
Originally Posted by mideastgirl Dormilich,
Are you suggesting, as is the link you sent me, that I need to use mysql_query() to see if the resource (variable/password?) I am using is a variable at all? first, every variable in PHP conforms to a data type (this type is not necessarily always the same—PHP is a so-called dynamically typed lanuage [ref.]) (that's got nothing to do with password, etc.). further, every (strictly speaking) function in PHP returns a value (this can be any of the alredy mentioned data types or "void" (no return value)). since even we experts do not know this stuff from all the 2,000+ PHP function we can look them up in the manual, where in the beginning a description is given what variables you must provide (minimum parameter number along with their type) and what the function returns (in which case).
example (how to read the manual*) Quote:
Description - resource mysql_query ( string $query [, resource $link_identifier ] )
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier .
Parameters
this means, you must provide a SQL sting as input and optionally the connection (resource) variable. you'll get a resultset (a resource type) return value (except otherwise stated in the "return values" section). optional parameters are set in square brackets ( [ ] ) Quote:
query Quote:
A SQL query
The query string should not end with a semicolon.
link_identifier Quote:
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level error is generated.
the description what each of the input parameters is. Quote:
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
as you can see, the mysql_query() function usually returns a ressource type (variable). but when reading under "return values" you'll see that this is not the only option, mysql_query() in general returns FALSE if there was an error (that is common for many functions) and sometimes it may return TRUE (both are boolean data types. and if your next function expects a resource input data type, it won't let you away if you provide a boolean (or any other)
* this is important as most errors can be explained at/tracked down to this level
will show you the content AND data type of the variable of interest (this is one of the #1 debugging functions)
you should also cross-read at wikipedia about programming languages, there are some basic termini (like "data type" or "return value") that make discussing bugs and programmes much easier.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,753
| | | re: Parse error: syntax error, unexpected $end in... Quote:
Originally Posted by mideastgirl Atli,
I have opened the document in Notepad ++ and some of the characters are in color, like semi-colons and such. However I do not know how to identify which characters are trash. Are the one's in color trash? Not really sure. I am going to mess with it more and hopefully figure it out. No, Notepad++ highlights the syntax in color so it is easier to read.
Junk characters usually show up as black squares. They are hidden, so this is just how Notepad++ (and a lot of other editors) represent them.
Pay close attention to the starting <?php tag. The rest isn't really important.
There must be something before it. A space, a piece of HTML, a junk char... anything.
If there is not, check the Format menu. If the Encode as UTF8 is selected, change it to the one above it: Encode as UTF8 without BOM.
Normal UTF8 documents have leading identifier chars that cause the problem you are seeing.
| | Newbie | | Join Date: Jul 2009
Posts: 1
| | | re: Parse error: syntax error, unexpected $end in...
Thanks so much for sharing the post.
<link removed: no unnecessary links, thanks>
|  | Expert | | Join Date: Mar 2007 Location: England
Posts: 1,083
| | | re: Parse error: syntax error, unexpected $end in...
....Has she gone :- !
| | Member | | Join Date: Feb 2009 Location: Romania/London
Posts: 88
| | | re: Parse error: syntax error, unexpected $end in...
I think she may have finally figured it out :D
|  | | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|