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 2
Oldest  Newest  Rating
0 Brandon Bennett · November 17, 2014
All that worked for me was 'works to here-1' and 'works to here-2'... 
0 Brandon Bennett · November 11, 2014
okay, thank you so much for your help! i replaced that bit of code, and this is what i got. This is for an actual website that i'm building. 


FUNCTION cpaeguru_coolplacesandevents.COUNT does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual


I've never heard of this 'referance manual' before.

My host that i have is godaddy if that helps...

Also, thank you so much, i decided that i wanted to learn coding and build something while i did it, so having an extremely helpful person there to ask like yourself is amazing. 
0 Brandon Bennett · November 22, 2014
what do i do if i have a separate "connect.php"?
0 Brandon Bennett · November 22, 2014
And what does this mean? i did the edits and got this

"Warning: mysql_query() expects parameter 1 to be string, resource given in /core/functions/users.php on line 13"
0 OLayemii Garuba · October 19, 2015
gotten a solution yet?
0 Ron Butcher · November 18, 2014
I think the next step is to start commenting out the inside of your first if statement from 

$username = $_POST['username'];


All the way to the end of the statement. (the } that closes the last if statement).
Run the code, then uncomment each if statement until the page fails.
Here is the entire code that I have been testing with.  I took all of the code you gave so far and put in into one file called test.php.  I removed the POST if statement and manually set the username and password variables (since I do not have a form to pass information from).  This code does work.

<?php
session_start();

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>';
}

function login($par1, $par2)
{
return true;
}

function user_exists($param)
{
return true;
}

function user_active($param)
{
return true;
}

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

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

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);

// Output should be:
// works to here-1works to here-2works to here-3works to here-4Array ( [user_id] => 1 )


//include 'includes/overall/footer.php';

?>
0 Teslim Salako · October 19, 2015
paste your code so we can see whats wrong
0 Ron Butcher · November 12, 2014
COUNT is a function in MySQL.  Basically it counts each of the results that it comes across, and instead of producing a list of results, it only returns the number of results it found.  The error is telling you that there is a problem with that function.

Try these two things.  Remove the quotes ('  ') in the COUNT paramater, and remove the space between COUNT and the ();


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


That should take care of you.
0 Brandon Bennett · November 18, 2014
okay, I did that i still got the same result on my end - however in the process of doing that, I discovered that when I don't enter a password on my form the 'should not see me-1' appears as well as the 'works to here-5' 

Does this mean my error is in a different file?
0 Ron Butcher · November 23, 2014
you can include the connect.php file with an include or require statement. That would have all of your $link information.
require 'connect.php';  

connect.php:
$link = mysqli_connect("host", "login_name", "login_password", "database");

The only real catch is that when you include this file at the top of your PHP page, you will not be able to use the $link variable in your functions without adding this line of code in each function that needs to run a query:
function example()
{
global $link; // This allows a variable that was defined outside the function
// Rest of function code i.e. $query = mysqli_query($link, "SELECT * FROM myTable");
}


On your error, it looks like you are mixing mysql and mysqli.  The mysql_query only expects a string that is the actual query, the mysqli_query expects two paramaters.  The first is the connect information, the second is the query.
Make sure that your query statement is as follows:
$query = mysqli_query($link, $qryString);  //  Do not forget the 'i' in mysqli

PHP

124,665 followers
About

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

Links
Moderators