Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356031
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356033
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356038
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356046
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356047
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356048
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356049
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264590-mysql_result-error/#findComment-1356050
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.