0

I know a similar question has been answered but with my case i'm not using booleans I get this error

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /home3/arm103/public_html/battle.php on line 19

here is battle.php from line 1 to 25

<?php
session_start();
include("header.php");
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{
  if(isset($_POST['gold'])) {
    $energy = protect($_POST['energy']);
    $id = protect($_POST['id']);
    $user_check = mysql_query("SELECT * FROM `stats` WHERE `id`='".$id."'") or        die(mysql_error());
if(mysql_num_rows($user_check) ==0) {
  output("There is no user with that ID!");
}elseif($energy < 1 || $energy > 10) {
  output("You must attack with 1 - 10 energy!");
}elseif($energy > $stats['energy']){
  output("You do not have enough energy!");
}elseif($id == $_SESSION['uid']) {
  output("You can not attack yourself!");
}elseif(mysql_num_rows($attacks_check) > 9) {
  output("This person has been already attacked the maximum of 10 times in the last 24 hours!");
}else{
    $enemy_stats = mysql_fetch_assoc($user_check);
    $attack_effect = $energy * 0.1 * $stats['attack'];
    $defence_effect = $enemy_stats['defence'];

It would be great if someone could show me the problem thanks

Sirko
  • 72,589
  • 19
  • 149
  • 183
Aidan
  • 13
  • 1
  • 5
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde May 15 '13 at 20:52
  • Where exactly would `$attacks_check` come from? :) – Joachim Isaksson May 15 '13 at 20:54

3 Answers3

2

$attacks_check is not defined, and is therefore null. You will need to initialize it as a resource before you can pass it as a parameter to mysql_num_rows().

}elseif(mysql_num_rows($attacks_check) > 9) {

As a side note, please be aware that the mysql_* functions are deprecated in favor of mysqli or PDO. Please consider updating your code to improve security and stability.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
2

There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.

You are referencing a variable that is not defined $attacks_check is not assigned and so is null.

Schleis
  • 41,516
  • 7
  • 68
  • 87
0

You used $attack_check but it was not definied

}elseif(mysql_num_rows($attacks_check) > 9) {

This is causing the error since variable is empty

Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

Fabio
  • 23,183
  • 12
  • 55
  • 64