Jump to content

[SOLVED] PHP Mysql Error


Akinzin

Recommended Posts

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

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'];

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.

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.

 

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'";

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.