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 3
Oldest  Newest  Rating
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 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 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 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 Brandon Bennett · November 6, 2014
Oh my gosh, haha that is killer! thank you so much for helping me!
+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.

PHP

107,321 followers
About

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

Links
Moderators