Akinzin Posted May 31, 2009 Share Posted May 31, 2009 Why do i get this error using this code? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\apply_check.php on line 20 <?php require_once('includes/config.php'); require_once('includes/functions.php'); $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $clankey = $_POST['keycode']; if (empty($username) OR empty($password) OR empty($email) OR empty($clankey) OR !ctype_alnum($username) OR !ctype_alnum($password) OR !ctype_alnum($clankey) OR $username > 20) { redirect("apply.php"); } else { $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $email = mysql_real_escape_string($email); $clankey = mysql_real_escape_string($clankey); $sql="SELECT * FROM clankeys WHERE key='$clankey'"; $result=mysql_query($sql, $conn); $count=mysql_num_rows($result); if($count==1) { echo "Key Found"; } else { echo "Key not found"; } } ?> Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/ Share on other sites More sharing options...
monkeytooth Posted May 31, 2009 Share Posted May 31, 2009 just a quick glance, assuming line 20 is $count=mysql_num_rows($result); then I am going to jump to the assumption that it could be your mysql version, I dont recall that way of counting rows working in newer versions of mySQL.. try something to the effect of... $ckeys_query = "SELECT COUNT(*) AS ckeys_rows FROM clankeys WHERE key='$clankey'"; $ckeys_result = mysql_query($ckeys_query) or die ("Error Clan Key Count: " . $query_settings . "<br />" . mysql_error()); $ckeys_row = mysql_fetch_array($ckeys_result, MYSQL_ASSOC); $count = $ckeys_row['ckeys_rows']; Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846150 Share on other sites More sharing options...
Jibberish Posted May 31, 2009 Share Posted May 31, 2009 also, where abouts are you connecting to the db? are you sure that its returning results? Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846154 Share on other sites More sharing options...
monkeytooth Posted May 31, 2009 Share Posted May 31, 2009 The connection scheme I didn't add, it is assumed to be using the existing connection that your using as there's no mention of the specific connection shown with yours. As for it pulling the result correctly, it works accordingly for my needs and hasn't let me down yet, and with that should work accordingly well with yours. Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846165 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 The connection scheme I didn't add, it is assumed to be using the existing connection that your using as there's no mention of the specific connection shown with yours. As for it pulling the result correctly, it works accordingly for my needs and hasn't let me down yet, and with that should work accordingly well with yours. Change this: $result=mysql_query($sql, $conn); To: $result=mysql_query($sql) or die("Failed: $sql <br>Reason: " . mysql_error()); This will help understand you about the actual issue. Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846270 Share on other sites More sharing options...
Akinzin Posted May 31, 2009 Author Share Posted May 31, 2009 I get this: Failed: SELECT * FROM clankeys WHERE key='4' Reason: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key='4'' at line 1 Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846380 Share on other sites More sharing options...
Ken2k7 Posted May 31, 2009 Share Posted May 31, 2009 Just food for thought in the future, don't use MySQL reserved words with table or column names. key is a MySQL reserved word. You can use backticks (`) to tell MySQL to not treat it as a reserved word, but it's recommended that you don't name your table or columns with MySQL reserved words. $ckeys_query = "SELECT COUNT(*) AS ckeys_rows FROM clankeys WHERE `key`='$clankey'"; Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846386 Share on other sites More sharing options...
Akinzin Posted May 31, 2009 Author Share Posted May 31, 2009 Oh! Thanks, just added the back ticks and it works! Link to comment https://forums.phpfreaks.com/topic/160347-solved-php-mysql-error/#findComment-846391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.