Jump to content

[SOLVED] MySQL Warnings - Please Help


jjacquay712

Recommended Posts

I have a PHP script that checks to see if a user name exists on a database. When MySQL gets a result everything is fine... the problem is when MySQL cant find anything.  Here Is My Code:

 

$resource2 = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}'");
$password = mysql_result($resource2, 0, 'password');
if ($password == $_POST['password']) {
echo "Correct Password<br /><br />";
} else {
echo "Wrong Password";
}

 

If the user enters a correct username, he is logged on, if they don't, they get this ugly warning: Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 3 in yada yada yada. Is there anyway to make MySQL not display the warning? I have tried an or die() statement, but that didn't work. I think you might be able to change the settings in MySQL but I'm not sure. Any suggestions are greatly appreciated.

Link to comment
Share on other sites

probs better off doing it more like this..

 

$resource2 = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' && password='{$_POST['password']}'");
if (mysql_num_rows($resource2) > 0) {
    print 'correct password!';
} else {
    print 'incorrect password!';
}

Link to comment
Share on other sites

1) Never hide error_reporting as a way to fix an error, because you're really fixing nothing at all.

2) Don't use mysql_result, since it's slower than other mysql retrieval functions.

3) Check out mysql_num_rows and just using WHERE in the query...

 

EDIT: MrAdam beat me to #3.

Link to comment
Share on other sites

If oyu mean a sort of true/false return from the mysql_query function you can use:

 

$query = mysql_query("...");

if ($query) { ... }

 

But to be honest I've found it a little unreliable in the past... If you don't mean that, then I don't know what you mean...

Link to comment
Share on other sites

I tried this:

 

$query = mysql_query("...");
if ($query) { ... }

 

and it seemed to work. ill probably do something like this to make sure the warning dosnt pop up:

 

$query = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}'");
if ($query) {
//retrieve data from database using  mysql_result()
}

 

Link to comment
Share on other sites

By only checkin the username in the query you are still selecting that record, then having to do tests on the password field value to check if you actually needed the record. Quicker way round is to just add password to the query which then only returns records with the right username and password.. Making code neater, easier to understand and most likely saving some load time...

 

If you've been getting an error did you try adding:

 

mysql_query("...") or die( mysql_error() );

 

??

Link to comment
Share on other sites

If oyu mean a sort of true/false return from the mysql_query function you can use:

 

$query = mysql_query("...");

if ($query) { ... }

 

But to be honest I've found it a little unreliable in the past... If you don't mean that, then I don't know what you mean...

 

MySQL returns a valid result resource as long as there are no errors in the query, even if no rows were returned.  That's horribly unreliable, and I don't quite understand why the thread starter asks us for help and then does his own thing anyway.  You need to add the POST variable for the password into the query and then use mysql_num_rows() to see if it matched any records.

Link to comment
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.