mroberts46 Posted June 22, 2012 Share Posted June 22, 2012 Anybody see what's wrong with this: return (mysql_result(mysql_query("SELECT COUNT (`id`) FROM `user` WHERE `username` = '$username'"), 0) == 1) ? true : false; I've looked over it several times and it appears to be right to me but maybe I put something wrong like maybe a '(' where there should be a '{' or '['. I keep getting this error: Warning: mysql_result(): supplied argument is not a valid MySQL result resource in ***/public_html/new_site/testing/core/functions/users.php on line 7 That line is line 7. I have the same structure for all of my functions so whatever is wrong here may be wrong in all of them. Please help. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/ Share on other sites More sharing options...
Drummin Posted June 22, 2012 Share Posted June 22, 2012 Maybe try ('id') instead of (`id`) return (mysql_result(mysql_query("SELECT COUNT('id') From user WHERE username ='$username'"), 0) == 1) ? true : false; Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356020 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 I tried it unsuccessfully. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356022 Share on other sites More sharing options...
Pikachu2000 Posted June 22, 2012 Share Posted June 22, 2012 echo mysql_error(); Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356024 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 Where? I'm relatively new to this. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356025 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 This is the error: FUNCTION dst_members.COUNT does not exist Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356029 Share on other sites More sharing options...
Drummin Posted June 22, 2012 Share Posted June 22, 2012 Just as an example, this works for me when I define $user as a name from a test DB. <?php $user="Bunny"; function dst_members($username){ return (mysql_result(mysql_query("SELECT COUNT('id') From user WHERE username ='$username'"), 0) == 1) ? true : false OR DIE ("Error: ".mysql_error()); } echo dst_members($user); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356031 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 I fixed the FUNCTION dst_members.COUNT does not exist error but I still have the Warning: mysql_result(): supplied argument is not a valid MySQL result resource in ***/public_html/new_site/testing/core/functions/users.php on line 7 error. I fixed the first error by removing the space from between COUNT and (`id`). I've still gotta figure out what's wrong with the rest of the code Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356033 Share on other sites More sharing options...
xyph Posted June 22, 2012 Share Posted June 22, 2012 Do you want to return the result of the query, or TRUE/FALSE ? Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356034 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 True/False Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356035 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 This is my current error: Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /***/public_html/new_site/testing/core/functions/users.php on line 7 xxyy This is my current code: <?php function logged_in() { return (isset($_SESSION['id'])) ? true : false; } function user_exists($username) { $username = sanitize($username); return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `user` WHERE `username` = '$username'") or die(mysql_error()), 0) == 1) ? true : false; } function user_active($username) { $username = sanitize($username); return (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `user` WHERE `username` = '$username' AND `active` = 1"), 0) == 1) ? true : false; } function user_id_from_username($username) { $username = sanitize($username); return mysql_result(mysql_query("SELECT `id` FROM `users` WHERE `username` = '$username'"), 0, '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(`id`) FROM `user` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false; } ?> This is the call to the functions listed here: <?php include 'core/init.php'; if (empty($_POST)=== false) { $username = $_POST['user']; $password = $_POST['password']; if (empty($username) === true || empty($password) === true) { $errors[] = 'You must enter username and password'; } else if (user_exists($username) === false) { $errors[] = 'Username not found'; } else if (user_active($username) === false) { $errors[] = 'You must activate your account before you may login'; } else { $login = login($username, $password); if ($login === false) { $errors[] = 'The username/password combination you have entered is incorrect'; } else { $_SESSION['id'] = $login; header('Location: index.php'); exit(); } } } else { $errors[] = 'No data received.'; } include 'includes/overall/header.php'; if (empty($errors) === false) { ?> <div id="container"> <h1>We tried to log you in but ...</h1> <?php echo output_errors($errors); } ?> </div> <?php include 'includes/overall/footer.php'; ?> Does anyone have any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356038 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 Can anyone help me? I'm at a complete loss. I'm new to this. I was following a tutorial and it worked there but not for me. I really don't know what else to do and I've got to have this system up and running by Sunday. Sadly, I'm still at the beginning. I was tossed into this with no life-raft and I'm sinking fast. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356046 Share on other sites More sharing options...
PFMaBiSmAd Posted June 22, 2012 Share Posted June 22, 2012 Your current error is because of the or die() statement that is part of that nested logic. When the mysql_query() produces a result resource and you logically or that value, rather than assign it to a variable, the value supplied to the outer mysql_result function is no longer a result resource. You should never nest functions that can fail due to an error, since it prevents proper error checking (did something fail or not), error reporting (output a user message and log the actual error so that you can find and fix problems), and error recovery (take an appropriate action in your code to recover from the error condition.) Nesting functions like that might produce 'cute' code, but it's poor programming. P.S. I removed the new thread you started for this same code/same error. Don't start new threads for the same problem. P.P.S. Programming requires a large amount of patience. Don't start madly bumping your thread. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356047 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 Thank you for the explanation. I needed that. I used the die() function to find an error and caused one by leaving it in. And I apologize for the duplicate posts. I'm beginning to feel the stress of taking on such a big project in such a small amout of time. Hopefully I can make it through the rest of this project a little smoother now. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356048 Share on other sites More sharing options...
Drummin Posted June 22, 2012 Share Posted June 22, 2012 Hey my bad for adding the or die() statement in my example. I still think the COUNT(`id`) should be straight single quotes, i.e. COUNT('id') My thought is that DB connection is not available within the function, but as I normally don't do queries in functions I can't say for sure. Again sorry if I mislead you. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356049 Share on other sites More sharing options...
mroberts46 Posted June 22, 2012 Author Share Posted June 22, 2012 Oh it's okay. I took out the die() function and it worked perfectly all the way through. I'm finally able to move forward again. I'm hoping not to run into too many more issues like this. I do learn from my mistakes and since I'm practically teaching myself as I go, I'm bound to make mistakes. I've learned 3 three different ways of returning query data so far and I'm sure I'll run into even more later on. Thanks for the help. The die() function did come in handy in finding a few typos. Just wish I had known to take it back out to alleviate the bigger error. Anyway, I can't thank you guys enough for your help. Quote Link to comment https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.