StefanPakaski Posted July 23, 2012 Share Posted July 23, 2012 I have trying to find what i am doing wrong but i cant find it. When i try to log in on my page i get this. Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /www/zzl.org/s/i/m/simago/htdocs/func/user.func.php on line 14 Here is the user.func script: <?php function logged_in() { return isset($_SESSION['user']); } session_start(); mysql_connect ('localhost', '********', '************'); mysql_select_db('*********'); function login_check($email, $password){ $email = mysql_real_escape_string($email); $password = mysql_real_escape_string($password); $login_query = mysql_query("SELECT COUNT(`user_id`) as `count`, `user_id` FROM `users` WHERE? `email` = '$email' AND `password` = '".md5($password)."' "); return (mysql_result($login_query, 0) == 1) ? mysql_result($login_query, 0, 'user_id') : false; } function user_data() { } function user_register($email, $name, $password) { $email = mysql_real_escape_string($email); $name = mysql_real_escape_string($name); mysql_query("INSERT INTO `users` VALUES ('', '$email', '$name', '".md5(password)."')"); return mysql_insert_id(); } function user_exists() { } ?> This is my line 14: return (mysql_result($login_query, 0) == 1) ? mysql_result($login_query, 0, 'user_id') : false; Please help! Quote Link to comment Share on other sites More sharing options...
Barand Posted July 23, 2012 Share Posted July 23, 2012 That error is symptomatic of a failed query. After calling mysql_query check the result from mysql_error(); if (!$login_query) echo mysql_error(); Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted July 23, 2012 Share Posted July 23, 2012 Yeah as above. I usually make the noob mistake of naming the database instead of the table in the query - which naturally makes it fail. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 What's actually doing a "?" question mark immediately after "WHERE" clause ? $login_query = mysql_query("SELECT COUNT(`user_id`) as `count`, `user_id` FROM `users` WHERE? `email` = '$email' AND `password` = '".md5($password)."' "); Quote Link to comment Share on other sites More sharing options...
StefanPakaski Posted July 24, 2012 Author Share Posted July 24, 2012 What's actually doing a "?" question mark immediately after "WHERE" clause ? $login_query = mysql_query("SELECT COUNT(`user_id`) as `count`, `user_id` FROM `users` WHERE? `email` = '$email' AND `password` = '".md5($password)."' "); It isn't there in the script so it must have been when i pasted it. I solved this problem but now i get another error. Warning: Wrong parameter count for mysql_result() in /www/zzl.org/s/i/m/simago/htdocs/func/user.func.php on line 16 <?php function logged_in() { return isset($_SESSION['user']); } session_start(); error_reporting(E_ALL); function login_check($email, $pass) { $email = mysql_real_escape_string($email); $login_query = mysql_query("SELECT COUNT (`user`) as `count`, `userid` FROM `users` WHERE `email`='$email' AND `pass`='".md5($pass)."'"); return (mysql_result ($login_query) == 1) ? mysql_result($login_query) : false; } function user_data() { } function user_register($email, $name, $pass) { $email = mysql_real_escape_string($email); $name = mysql_real_escape_string($name); mysql_query("INSERT INTO `users` VALUES ('', '$email', '$name', '".md5($pass)."')"); return mysql_insert_id(); } function user_exists() { } ?> My line 16 is: ($login_query) == 1) ? I am new to php so please give an answer that i might understand. Thanks folks! Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 24, 2012 Share Posted July 24, 2012 line 16 includes everything until the ; use the manual to check these things. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 24, 2012 Share Posted July 24, 2012 mysql_result requires AT LEAST 2 parameters, there is an optional 3rd. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 24, 2012 Share Posted July 24, 2012 Try: return ($login_query == 1) ? mysql_result($login_query) : false; Quote Link to comment Share on other sites More sharing options...
StefanPakaski Posted July 24, 2012 Author Share Posted July 24, 2012 I cant thank you enough, jazzman1! <3 Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 24, 2012 Share Posted July 24, 2012 That should not work, cannot believe that it does. mysql_result REQUIRES 2 arguments, the resource, and the row. The field is optional. I normally do not repeat myself, but you are just setting yourself up for problems in the future, as this is bad coding practices. (*NOTE: when viewing the manual, any arguments surrounded in [ and ] is optional, they will also have the default value given, in this case it is 0 for the 3rd argument). //at the very least, gives you the count. mysql_result($login_query,0); //if you want the userid; mysql_query($login_query,0,1); I personally would also check to see if the query returned rows, instead of checking to make sure it wasn't false. Not real sure why you would check to see if a result resource was equal to 1, when it could never equal 1 since it is a result resource. It can exists, therefore equal TRUE, or not exist therefore equal FALSE. Once again, that is my personal taste, I've seen plenty of "coders" that check it the way you have, I just don't agree that it is right. Quote Link to comment Share on other sites More sharing options...
StefanPakaski Posted July 24, 2012 Author Share Posted July 24, 2012 I have followed some of the tutorials to phpAcademy. Dunno exactly what i am doing :S Quote Link to comment 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.