Jump to content

Undefined Object Variable


MasterACE14

Recommended Posts

I have a page pagination script which is poorly coded to say the least. It is throwing me this notice...

Notice: Undefined variable: usersA in /home/eliteace/public_html/realmbattles_dev/test.php on line 110

 

<?php

// connect to database etc

error_reporting(E_ALL);


function getUserDetails($id, $fields = "*")
  {
      $str = "select $fields from `UserDetails` where  ID='$id' ";
      //echo $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return 0;
      } else {
          
          $st = "";
          $st = mysql_fetch_object($q);
          $st->untrainedSold = floor($st->untrainedSold);
          return $st;
      }
  }

////////////////////

function getRanksList($page)
  {
      global $conf;
      if($page == 0) $page = 2;
      $start = ($page - 1) * $conf['users_per_page'];
      $str = "SELECT id,Rank FROM `AllRanks` WHERE Rank>0  ORDER BY `Rank` ASC LIMIT $start,{$conf['users_per_page']}";
      //print $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return;
      } else {
          
          $st = "";
          $i = 0;
          while ($row = mysql_fetch_object($q)) {
              $st[$i] = $row;
              $i++;
          }
          return $st;
      }
  }
  
  function getRanksUsersList($page, $fields = " ID, userName, trainedAttackSold, trainedAttackMerc, trainedDefSold, trainedDefMerc, untrainedSold, untrainedMerc, spies, race, gold ")
  {
      //global $conf;
      $users = getRanksList($page);
      for ($i = 0; $i < count($users); $i++) {
          $usersA[$i] = getUserDetails($users[$i]->userID, $fields);
          $usersA[$i]->Rank = $users[$i]->Rank;
      }
      return $usersA; // line 110
  }

// Test File
$users = getRanksUsersList($cgi['page']);

  for ($i = 0; $i < count($users); $i++)
    {
    
	$users[$i]->ID."<br />";

    }

?>

 

The function getUserDetails works everywhere else in the site, Don't know why it won't work here.

 

Any help is GREATLY appreciated.

 

Regards ACE

 

Link to comment
Share on other sites

It appears as though you have not defines $usersA in the script you have supplied. Try defining it. This message is a notice, which will only appear when you have error_reporting(E_ALL); which you have. You will need to declare the variable $usersA.

Link to comment
Share on other sites

I don't see why it isn't being declared.

<?php
  function getRanksUsersList($page, $fields = " ID, userName, trainedAttackSold, trainedAttackMerc, trainedDefSold, trainedDefMerc, untrainedSold, untrainedMerc, spies, race, gold ")
  {
      //global $conf;
      $users = getRanksList($page);
      for ($i = 0; $i < count($users); $i++) {
          $usersA[$i] = getUserDetails($users[$i]->userID, $fields);
          $usersA[$i]->Rank = $users[$i]->Rank;
      }
      return $usersA; // line 110
  }

if it's getting the users info and looping through all users in the database and assigning that user 'object' to $usersA then it should be defined.

 

But it's not? ???

Link to comment
Share on other sites

Just create it in that function as an empty array before you use it.

 

<?php
  function getRanksUsersList($page, $fields = " ID, userName, trainedAttackSold, trainedAttackMerc, trainedDefSold, trainedDefMerc, untrainedSold, untrainedMerc, spies, race, gold ")
  {
      //global $conf;
      $usersA = array();
      $users = getRanksList($page);
      for ($i = 0; $i < count($users); $i++) {
          $usersA[$i] = getUserDetails($users[$i]->userID, $fields);
          $usersA[$i]->Rank = $users[$i]->Rank;
      }
      return $usersA; 
  }
?>

 

Ken

Link to comment
Share on other sites

That fixed the notice but results still wouldn't show up when echo'ing them. I've changed the script abit and I'm now getting...

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

Script is Working... Correctly? not sure.

 

code...

<?php

error_reporting(E_ALL);


function getUserDetails($id, $fields = "*")
  {
      $str = "select $fields from `UserDetails` where  ID='$id' ";
      //echo $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return 0;
      } else {
          
          $st = "";
          $st = mysql_fetch_object($q);
          $st->untrainedSold = floor($st->untrainedSold);
          return $st;
      }
  }

////////////////////

function getRanksList($page)
  {
      global $conf;
      if($page == 0) $page = 2;
      $start = ($page - 1) * $conf['users_per_page'];
      $str = "SELECT id,Rank FROM `AllRanks` WHERE Rank>0  ORDER BY `Rank` ASC LIMIT $start,{$conf['users_per_page']}";
      //print $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return;
      } else {
          
          $st = "";
          $i = 0;
          while ($row = mysql_fetch_object($q)) {
              $st[$i] = $row;
              $i++;
          }
          return $st;
      }
  }
  
  function getRanksUsersList($page, $fields = " ID, userName, trainedAttackSold, trainedAttackMerc, trainedDefSold, trainedDefMerc, untrainedSold, untrainedMerc, spies, race, gold ")
  {
      //global $conf;
      $usersA = array();
      $users = getRanksList($page);
      for ($i = 0; $i < count($users); $i++) {
          $usersA[$i] = getUserDetails($users[$i]->ID, $fields);
          $usersA[$i]->Rank = $users[$i]->Rank;
      }
      return $usersA;
  }

// Test File
$users = getRanksUsersList($_GET['page']);

  for ($i = 0; $i < count($users); $i++)
    {
    
	$users[$i]->ID."<br />";

    }
    
$users2 = getRanksList($_GET['page']);

  for ($i = 0; $i < count($users2); $i++)
    {
    
	$users2[$i]->ID."<br />";

    }

echo "Script is Working... Correctly? not sure."

?>

Link to comment
Share on other sites

The full test.php is...

<?php

/**
* @author ACE
* @copyright 2009
*/

include("config/connect.php");

  function realm() 
  {
 // workout what realm this is
 if(!file_exists("realm.txt")) {
 	die("Realm file is not present!");	
 }
 $file = fopen("realm.txt","r") or die("Unable to Figure out what Realm this is!");
 while(!feof($file))
  	 {
		 $content = fgets($file);
	 }
 fclose($file);
 return $content;
  }



 if(realm() == "main") {
 	$mysql['database'] = "eliteace_realmbattles";	
 } elseif(realm() == "test") {
 	$mysql['database'] = "eliteace_realmbattlesdev";	
 }

 $conf["sites_database_name"] = $mysql['database']; 
 $conf["sites_database_login"] = $mysql['user']; 
 $conf["sites_database_password"] = $mysql['pass'];
 $conf["path"] = $_SERVER['SERVER_NAME'];

include("lib.php");

  $db = @mysql_connect('localhost', $conf["sites_database_login"], $conf["sites_database_password"]);
  if (!$db) {
    die(mysql_error());
  }
  if (!@mysql_select_db($conf["sites_database_name"], $db)) {
      $str = mysql_error();
      if (isset($str)) {
        die($str);
      }
  }

// error_reporting(E_ALL);


function getUserDetails($id, $fields = "*")
  {
      $str = "select $fields from `UserDetails` where  ID='$id' ";
      //echo $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return 0;
      } else {
          
          $st = "";
          $st = mysql_fetch_object($q);
          $st->untrainedSold = floor($st->untrainedSold);
          return $st;
      }
  }

////////////////////

function getRanksList($page)
  {
      global $conf;
      if($page == 0) $page = 2;
      $start = ($page - 1) * $conf['users_per_page'];
      $str = "SELECT id,Rank FROM `AllRanks` WHERE Rank>0  ORDER BY `Rank` ASC LIMIT $start,{$conf['users_per_page']}";
      //print $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return;
      } else {
          
          $st = "";
          $i = 0;
          while ($row = mysql_fetch_object($q)) {
              $st[$i] = $row;
              $i++;
          }
          return $st;
      }
  }
  
  function getRanksUsersList($page, $fields = " ID, userName, trainedAttackSold, trainedAttackMerc, trainedDefSold, trainedDefMerc, untrainedSold, untrainedMerc, spies, race, gold ")
  {
      //global $conf;
      $usersA = array();
      $users = getRanksList($page);
      for ($i = 0; $i < count($users); $i++) {
          $usersA[$i] = getUserDetails($users[$i]->ID, $fields);
          $usersA[$i]->Rank = $users[$i]->Rank;
      }
      return $usersA;
  }
  
  function searchRanksUsersListCount($str)
  {
      $str = "SELECT COUNT(*)  FROM `UserDetails` INNER JOIN `AllRanks` ON UserDetails.ID = AllRanks.id WHERE Rank>0 AND active='1' AND userName LIKE '$str'";
      //echo $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if ($q) {
          $st = mysql_fetch_array($q);
          return $st[0];
      } else {
          return 0;
      }
  }
  function searchRanksUsersList($page, $str)
  {
      global $conf;
      $start = ($page + 1) * $conf['users_per_page'];
      $str = "SELECT  `AllRanks`.`id`, `UserDetails`.`ID`, `AllRanks`.`Rank`, `UserDetails`.`userName`, `UserDetails`.`active` FROM `UserDetails`,`AllRanks` WHERE `UserDetails`.`ID` = `AllRanks`.`id` AND Rank>0 AND active='1' AND userName LIKE '$str' ORDER BY `AllRanks`.`Rank` ASC ";
      //print $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          return;
      } else {
          
          $st = "";
          $i = 0;
          while ($row = mysql_fetch_object($q)) {
              $row->untrainedSold = floor($row->untrainedSold);
              $st[$i] = $row;
              $i++;
          }
          return $st;
      }
  }
  function getUserRanks($id)
  {
      $str = "SELECT * FROM `AllRanks` where  `id`='$id'";
      //echo $str;
      $q = @mysql_query($str);
      if (!$q) {
          print('Query failed: ' . mysql_error());
          return;
      }
      if (!@mysql_num_rows($q)) {
          $st->Rank = 'unranked';
          $st->SARank = 'unranked';
          $st->DARank = 'unranked';
          $st->CRank = 'unranked';
          return $st;
      } else {
          
          $st = "";
          $st = mysql_fetch_object($q);
          if (!$st->Rank) {
              $st->Rank = 'unranked';
          }
          if (!$st->SARank) {
              $st->SARank = 'unranked';
          }
          if (!$st->DARank) {
              $st->DARank = 'unranked';
          }
          if (!$st->CRank) {
              $st->CRank = 'unranked';
          }
          return $st;
      }
  }

// Test File
$users = getRanksUsersList($_GET['page']);

  for ($i = 0; $i < count($users); $i++)
    {
    
	$users[$i]->ID."<br />";

    }
    
$users2 = getRanksList($_GET['page']);

  for ($i = 0; $i < count($users2); $i++)
    {
    
	$users2[$i]->ID."<br />";

    }

echo "Script is Working... Correctly? not sure."

?>

 

and lib.php that is included contains the $conf array.

$conf['users_per_page'] = 50;

 

`AllRanks` table structure is...

CREATE TABLE IF NOT EXISTS `AllRanks` (
  `id` int(11) NOT NULL default '0',
  `Rank` bigint(225) unsigned NOT NULL default '0',
  `SARank` bigint(225) unsigned NOT NULL default '0',
  `DARank` bigint(225) unsigned NOT NULL default '0',
  `CRank` bigint(225) unsigned NOT NULL default '0',
  `IRank` bigint(255) unsigned NOT NULL default '0',
  `random` int(11) NOT NULL default '1',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 

output:

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

 

Warning: Attempt to assign property of non-object in /home/eliteace/public_html/realmbattles_dev/test.php on line 109

Script is Working... Correctly? not sure.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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