it's correct
Main Topics
Browse All TopicsHello,
Is the syntax correct for the following line of code:
if ($result = mysql_query("SELECT user_name FROM tbl_logged_user WHERE user_name = '$user'"))
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
> the test will return true if some records are returned (eg if the user has been found in the database).
This is wrong, It will test wether the SQL-Statement has been executed successful (no syntax error), but it will not test how many records the resultset will have. So you need to add an additional codition:
if ($result = mysql_query("SELECT user_name FROM tbl_logged_user WHERE user_name = '$user'") && mysql_num_rows($result) > 0)
Is $user $_POST or $_GET or $_COOKIE variable or something else?
Try to use $_POST[user] or $_GET[user] or $_COOKIE[user] accordingly instead of $user if this is a case.
Ideally, the code will be:
if ($result = mysql_query(" SELECT `user_name` FROM `tbl_logged_user` WHERE `user_name` = '$_POST[user]' ")) {
echo "success";
} else {
echo "failure";
}
or
$result = mysql_query(" SELECT `user_name` FROM `tbl_logged_user` WHERE `user_name` = '$_POST[user]' ");
if (mysql_num_rows($result)>0
echo "success, found records";
} else {
echo "failure, no records";
}
make sure that table and field names are correct, you should have field `user_name` in the table `tbl_logged_user`
Good luck.
Business Accounts
Answer for Membership
by: elfe69Posted on 2007-07-17 at 02:54:42ID: 19503348
Yes, your syntax is correct and the test will return true if some records are returned (eg if the user has been found in the database).