jjacquay712 Posted September 3, 2008 Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/ Share on other sites More sharing options...
dropfaith Posted September 3, 2008 Share Posted September 3, 2008 error_reporting(0); you could hide those Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633217 Share on other sites More sharing options...
jjacquay712 Posted September 3, 2008 Author Share Posted September 3, 2008 thanks, ill try it out Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633218 Share on other sites More sharing options...
jjacquay712 Posted September 3, 2008 Author Share Posted September 3, 2008 Parse error: syntax error, unexpected T_STRING in /home/johnj/public_html/comments/index.php on line 33 hmmmmm... didnt work Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633219 Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 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!'; } Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633221 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633225 Share on other sites More sharing options...
jjacquay712 Posted September 3, 2008 Author Share Posted September 3, 2008 I tried using mysql_num_rows() but all it returned was a number. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633236 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 Yeah, it is a number...that's the point. What are you looking for? Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633238 Share on other sites More sharing options...
jjacquay712 Posted September 3, 2008 Author Share Posted September 3, 2008 im looking for the result of the query inside the variable edit: i tried it one more time, and it seems that it has the same problem of printing a warning if the resource dosn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633239 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 You should use the POST variable in the query, as MrAdam did in his example. And it shouldn't show that error...what's the exact error? Also, add some error checking to your query... Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633245 Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633248 Share on other sites More sharing options...
jjacquay712 Posted September 3, 2008 Author Share Posted September 3, 2008 i did use the post variable in the query: SELECT * FROM users WHERE username='{$_POST['username']}'. Ill try to use what mradam suggested. $query = mysql_query("..."); if ($query) { ... } Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633250 Share on other sites More sharing options...
jjacquay712 Posted September 3, 2008 Author Share Posted September 3, 2008 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() } Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633254 Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 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() ); ?? Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633258 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122635-solved-mysql-warnings-please-help/#findComment-633263 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.