Parse Error?

+4 Brandon Bennett · November 5, 2014
I'm having some trouble and i'm not sure what a parse error is. 

I keep getting this error: 

"Parse error: syntax error, unexpected 'return' (T_RETURN) in /functions/users.php on line 14" and I'm not sure what i'm missing.

Here is the code i'm using:

<?php
function user_data($user_id){ 
$data = array();

}

function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}

function user_exists() {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT ('user_id') FROM `users` WHERE `username` = '$username'")
return (mysql_result($query, 0) ==1) ? true : false;
}

function user_active() {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT ('user_id') FROM `users` WHERE `username` = '$username' AND `active` = 1")
return (mysql_result($query, 0) ===1) ? true : false;
}

function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0 'user_id');
}

function login($username, $password) {
$user_id = user_id_from_username($username);

$username = sanitize($username);
$password = md5($password);

return (mysql_result(mysql_query("SELECT COUNT('user_id') FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) ==1) ? $user_id : false;
}
?>

Please let me know if there is anything that i can do to fix this. 

Thank you!

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+2 Ron Butcher · November 6, 2014
The killer of all programmers got you.  You forgot a semicolon on line 13.

$query = mysql_query("SELECT COUNT ('user_id') FROM `users` WHERE `username` = '$username'");  <--  Right Here

Your going to need one on line 19 as well.
0 Brandon Bennett · November 7, 2014
one more... 

i keep getting this error: 

Warning: mysql_result() expects parameter 1 to be resource, boolean given in /functions/users.php on line 14

what does this mean?
0 Ron Butcher · November 8, 2014
That means that the MySQL query failed when it tried to run.  So instead of returning a set of results, it returned "false."  You need to find out what the error is before we can fix it.  Change your query to this:


$query = mysql_query("SELECT COUNT ('user_id') FROM `users` WHERE `username` = '$username'") or die(mysql_error());


What will happen, is the code will run, and when your query fails, the "or die()" statement will cause the entire page to immediately die and print out the MySQL error on the screen.  If you cannot figure out what the error is, put it here along with the `users` table layout.



Quick side note:  Most of the tutorials that are out there for PHP still use mysql for running database queries.  Unfortunately mysql is depreciated and is going to be removed in a future update of PHP.  You should look into changing your syntax to mysqli instead (MySQL Improved).  The syntax is almost the exact same so it won't be hard to make the switch.  
0 Brandon Bennett · November 16, 2014
Sweet! thank you, i have one more question if you don't mind. I am having some trouble when i try to log on. i put my user and password in and it doesn't redirect me to the index.php like i've told it to. 

Here's my code for 'login.php'


<?php
include 'core/init.php';
include 'includes/overall/header.php';

if (empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) === true || empty($password) === true) {
$errors[] = 'You need to enter a username and password';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find that username. Have you registered?';
} else if (user_active($username) === false){
$errors[] = 'Activation required first!';
} else {
$login = login($username, $password);
if ($login === false) {
$errors[] = 'Username or password is incorrect';
} else {
$_SESSION['user_id'] = $login;
header('Location: index.php');
exit();
}
}
}
echo output_errors($errors);

include 'includes/overall/footer.php';

?>

Am i missing anything or do you have any suggestions?
0 Ayodele Joseph · October 6, 2015
Hello guyz.... I have the same similar problem but have corrected it using this corrections seen here. The problem am having now is that my $data is not submitted to the my database......Plz, i need way forward.
0 Ron Butcher · November 16, 2014
Can you post your output_errors function?
0 Brandon Bennett · November 16, 2014
it doesn't display anything. it just stays at login.php instead of redirecting me to index.php
0 Brandon Bennett · November 16, 2014
here is output_errors in 'general.php'


<?php
function sanitize($data){
return mysql_real_escape_string($data);
}

function output_errors($errors) {
$output = array();
foreach ($errors as $error) {
$output[] = '<li>'. $error. '</li>';
}
return '<ul>' . implode('', $output) . '</ul>';
}
?>
0 Ron Butcher · November 16, 2014
I ran the code on my end and it did work.  Lets throw some echo statements into the mix to troubleshoot where it is failing.  Try this:


echo 'works to here-1';
if (empty($_POST) === false) {
echo 'works to here-2';
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) === true || empty($password) === true) {
$errors[] = 'You need to enter a username and password';
echo 'should not see me-1';
} else if (user_exists($username) === false) {
$errors[] = 'We can\'t find that username. Have you registered?';
echo 'should not see me-2';
} else if (user_active($username) === false){
$errors[] = 'Activation required first!';
echo 'should not see me-3';
} else {
echo 'works to here-3';
$login = login($username, $password);
if ($login === false) {
$errors[] = 'Username or password is incorrect';
echo 'should not see me-4';
} else {
$_SESSION['user_id'] = $login;
echo 'works to here-4';
print_r($_SESSION); <- Print all session variables to see if they are being set
//header('Location: index.php'); <- Commented this out as a temp test
exit();
}
}
}
echo 'works to here-5';
echo output_errors($errors);

 I know it looks a little funky, but what we are doing is breaking down each logical step to see where we are having issues.  You should see five "works to here" statements with numbers.  If you are missing a number, or it stops somewhere, we know what section to look for.

Just remember to remove all the extra crap before publishing your website.
0 Ron Butcher · November 20, 2014
He does need to check the $_POST array to see if it is empty.  If it is empty, and he tries to set $username to a POST variable that does not exist, the page will crash.  He could also put an else statement after checking the POST array to show the log in form if POST is empty.

PHP

107,280 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators