469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Parse error: syntax error, unexpected $end in...

mideastgirl
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //Connect To Database
  3. $hostname='mideasthonors2.db.4381361.hostedresource.com';
  4. $username='**************';
  5. $password='***********';
  6. $dbname='mideasthonors2';
  7. $usertable='admin_tasks';
  8. $yourfield = 'Name';
  9.  
  10. mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
  11. mysql_select_db($dbname);
  12.  
  13. $query = 'SELECT * FROM $usertable';
  14. $result = mysql_query($query);
  15. if($result) {
  16.     while($row = mysql_fetch_array($result)){
  17.         $name = $row['$yourfield'];
  18.         echo 'Name: '.$name;
  19.  
  20. mysql_select_db("mideasthonors2");
  21.  
  22. $sql="INSERT INTO $usertable (Name, Address 1, Address 2, City, State, Zip Code, Website, Contact Name, Position, Phone Number, Email Address)
  23.  
  24. VALUES
  25.  
  26. {$_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']};
  27.  
  28. if (!mysql_query($sql,$con))
  29.  
  30.   {
  31.  
  32.   die('Error: ' . mysql_error());
  33.  
  34.   }
  35.  
  36. echo '1 record added';
  37.  
  38. mysql_close($con);
  39.     }
  40. }    
  41. ?>
Jun 23 '09
68 10269
hoopy
88
There must be some trailing spaces or output before. Make sure you dont have a blank line at the top ie:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. echo ("hi");
  4. ?>
  5.  
It has to be:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo ("hi");
  3. ?>
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.
Jul 7 '09 #51
hoopy
88
In fact attach the files so we can see where the output is being generated.
Jul 7 '09 #52
Code from adminloginprocess.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("admin-dbcon.php");
  3.  
  4. // you need this to be able to use sessions
  5. session_start();  
  6.  
  7. //sanitize the user input to ensure random usernames cannot be used
  8. $sUsername= mysql_real_escape_string($_POST['Username']);
  9. $sPassword = mysql_real_escape_string($_POST['Password']);
  10.  
  11. //ensure username and password are valide
  12. $qry = mysql_query(" 
  13. SELECT * from admin_login 
  14. WHERE = username = '$sUsername' 
  15. AND password = '$sPassword' 
  16. ");
  17.  
  18. // check the number of rows returned 
  19. if(mysql_num_rows($qry) == 1)
  20. {
  21. // not a valid login redirect to fail 
  22. header("Location: login-error.php");
  23. exit;
  24. }
  25.  
  26. // if you get here then you have made a match 
  27. // store data in session variables 
  28. $row = mysql_fetch_array($qry);
  29. $_SESSION['username'] = $row['username'];
  30. $_SESSION['userid'] = $row['ID'];
  31.  
  32. // redirect user onto admintasks.php
  33. header("Location: admintasks.php");
  34. ?>
  35.  
**No spaces are shown

admin-dbcon.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbHost = 'mideasthonors.db.4381361.hostedresource.com';
  3. $dbName = 'xxx';
  4. $dbUser = 'xxx;
  5. $dbPass = 'xxx';
  6.  
  7. mysql_connect("$dbHost","$dbUser","$dbPass") or die("Error: Unable to connect to database! Please try again later.");
  8.  
  9. //Select the database we want to use
  10. mysql_select_db($dbName) or die("Error: Could not select database");
  11. ?>
  12.  
Jul 7 '09 #53
dlite922
1,584 Expert 1GB
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
Jul 7 '09 #54
hoopy
88
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.
Jul 7 '09 #55
Atli
5,058 Expert 4TB
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.
Jul 8 '09 #56
Thank you all for your suggestions. I have applied all of them and here is my code for adminloginprocess.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start()
  3. include("admin-dbcon.php");
  4.  
  5. // you need this to be able to use sessions
  6. session_start();  
  7.  
  8. //sanitize the user input to ensure random usernames cannot be used
  9. $sUsername= mysql_real_escape_string($_POST['Username']);
  10. $sPassword = mysql_real_escape_string($_POST['Password']);
  11.  
  12. //ensure username and password are valide
  13. $qry = mysql_query(" 
  14. SELECT * from admin_login 
  15. WHERE = username = '$sUsername' 
  16. AND password = '$sPassword' 
  17. ");
  18.  
  19. // check the number of rows returned 
  20. if(mysql_num_rows($qry) == 0)
  21. {
  22.  
  23. // not a valid login redirect to fail 
  24. header("Location: login-error.php");
  25. exit;
  26. }
  27.  
  28. // if you get here then you have made a match 
  29. // store data in session variables 
  30. $row = mysql_fetch_array($qry);
  31. $_SESSION['username'] = $row['username'];
  32. $_SESSION['userid'] = $row['ID']; 
  33. // redirect user onto admintasks.php
  34. header("Location: admintasks.php");
  35. ob_end_flush()
  36. ?>
  37.  
And my code for admin-db.com.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dbHost = 'mideasthonors.db.4381361.hostedresource.com';
  3. $dbName = 'xxx';
  4. $dbUser = 'xxx';
  5. $dbPass = 'xxx';
  6.  
  7. mysql_connect("$dbHost","$dbUser","$dbPass") or die("Error: Unable to connect to database! Please try again later.");
  8.  
  9. //Select the database we want to use
  10. mysql_select_db($dbName) or die("Error: Could not select database");
  11.  
***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:(
Jul 8 '09 #57
Dormilich
8,652 Expert Mod 8TB
@mideastgirl
I remember vaguely saying something about unexpected T_*** errors...
@Dormilich
Jul 8 '09 #58
hoopy
88
Your missing semi-colons after:

ob_start() and ob_end_flush()

Make sure they are:

Expand|Select|Wrap|Line Numbers
  1. ob_end_flush(); and ob_start();
Jul 8 '09 #59
Dormilich
8,652 Expert Mod 8TB
@hoopy: cheater ;)

.......
Jul 8 '09 #60
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. include("admin-dbcon.php");
  4.  
  5. // you need this to be able to use sessions
  6. session_start();  
  7.  
  8. //sanitize the user input to ensure random usernames cannot be used
  9. $sUsername= mysql_real_escape_string($_POST['Username']);
  10. $sPassword = mysql_real_escape_string($_POST['Password']);
  11.  
  12. //ensure username and password are valide
  13. $qry = mysql_query(" 
  14. SELECT * from admin_login 
  15. WHERE = username = '$sUsername' 
  16. AND password = '$sPassword' 
  17. ");
  18.  
  19. // check the number of rows returned 
  20. if(mysql_num_rows($qry) == 0)
  21. {
  22.  
  23. // not a valid login redirect to fail 
  24. header("Location: login-error.php");
  25. exit;
  26. }
  27.  
  28. // if you get here then you have made a match 
  29. // store data in session variables 
  30. $row = mysql_fetch_array($qry);
  31. $_SESSION['username'] = $row['username'];
  32. $_SESSION['userid'] = $row['ID']; 
  33. // redirect user onto admintasks.php
  34. header("Location: admintasks.php");
  35. ob_end_flush();
  36. ?>
  37.  
Jul 8 '09 #61
Dormilich
8,652 Expert Mod 8TB
@mideastgirl
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):
Expand|Select|Wrap|Line Numbers
  1. $qry = mysql_query($sql) or die(mysql_error());
Jul 8 '09 #62
Atli
5,058 Expert 4TB
@mideastgirl
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.
Jul 8 '09 #63
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.
Jul 9 '09 #64
Dormilich
8,652 Expert Mod 8TB
@mideastgirl
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*)

Description
Expand|Select|Wrap|Line Numbers
  1. 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 ( [ ] )

query

A SQL query

The query string should not end with a semicolon.
link_identifier

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.

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

Expand|Select|Wrap|Line Numbers
  1. var_dump($unknown_var);
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.
Jul 9 '09 #65
Atli
5,058 Expert 4TB
@mideastgirl
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.
Jul 9 '09 #66
Thanks so much for sharing the post.


<link removed: no unnecessary links, thanks>
Jul 10 '09 #67
code green
1,726 Expert 1GB
....Has she gone :- !
Jul 13 '09 #68
hoopy
88
I think she may have finally figured it out :D
Jul 13 '09 #69

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Janwillem Borleffs | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.