Returning 1 result

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Returning 1 result

Post by Aleeious »

This is returning 2 different results"

Code: Select all

<?php

class Match
{
	// constant for open Match
	const STATUS_OPEN = 0;

	// constant for completed Match
	const STATUS_COMPLETED = 1;

	// constant for cancelled Match
	const STATUS_CANCELLED = 2;

	// constant for match that was result of error
	const STATUS_ERROR = 3;

	// match id
	private $matchID = array();

	// challenger of match
	private $challenger = array();

	// defender of match
	private $defender = array();

	// instance of database
	protected $db;

	/**
	* class constructor
	* @param $db database class instance
	*/
    public function __construct(Database $db)
    {
        // get instance of database
        $this->db = $db;
    }

	/**
	* returns match id array
	* @return match id array
	*/
	public function getID()
	{
		return $this->matchID;
	}

	/**
	* returns challenger array
	* @return challenger array
	*/
	public function getChallengers()
	{
		return $this->challenger;
	}

		/**
	* returns defender array
	* @return defender array
	*/
	public function getDefenders()
	{
		return $this->defender;
	}

	/**
	* adds the attack pattern from the challenger to the database
	* @param $challenger's attack/block pattern as attack:block
	*/
	public function add($pattern)
	{
		// prepare the sql statement
		$statement = $this->db->prepare('INSERT INTO ' . TABLE_MATCHES . ' VALUES (?,?)');

		// bind the variables
		$statement->bind_param('ss', $pattern, "");

		// if the statement executed successfully
		if ($statement->execute())
		{
			// get the number of results
			$statement->bind_result($row);

			// fetch the results
			$statement->fetch();

			// if the user was added successfully
			if($row)
			{
				// return true
				return true;
			}

			// otherwise return false
			return false;
        }
	}

    /**
	* adds a match index to the database
	* @param $challenger the person initiating the challenge
	* @param $defender the person being challenged
	*/
	public function addIndex($challenger, $defender)
	{
		// prepare the sql statement
		$statement = $this->db->prepare('INSERT INTO ' . TABLE_MATCHESINDEX . ' VALUES (?,?,?)');

		// bind the variables
		$statement->bind_param('sss', $challenger, $defender, self::STATUS_OPEN);

		// if the statement executed successfully
		if ($statement->execute())
		{
			// get the number of results
			$statement->bind_result($row);

			// fetch the results
			$statement->fetch();

			// if the user was added successfully
			if($row)
			{
				// return true
				return true;
			}

			// otherwise return false
			return false;
        }
	}

    /**
	* counts the number of pending matches a player has
	* @param $username username to check number of pending challenges
	*/
	public function CheckPendingMatches($username)
	{
		// prepare the sql statement to change the password
        $statement = $this->db->prepare("SELECT id FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? OR defender = ? LIMIT 5");

		// convert constant to variable
		$status = self::STATUS_OPEN;

        // bind the variables
        $statement->bind_param('ssi', $username, $username, $status);

        // if the statement executed successfully
        if ($statement->execute())
        {
            // store the result
            $statement->store_result();

            // get the number of results
            $totalPendingMatches = $statement->num_rows;

            // return the total number of pending matches
            return $totalPendingMatches;
        }

        // query didn't execute so return false
        return false;
	}

	/**
	* get the current matches the playerhas pending
	* @username username to check for matches for
	*/
	public function getInfo($username)
	{
		// prepare the sql statement to change the password
        $statement = $this->db->prepare("SELECT id, challenger, defender FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? or defender = ? LIMIT 5");

		// convert constant to variable
		$status = self::STATUS_OPEN;

        // bing the variables
        $statement->bind_param('ssi', $username, $username, $status);

        // if the statement executed successfully
        if ($statement->execute())
        {
        	// get the number of results
			$statement->bind_result($id, $challenger, $defender);

			// fetch all the results
			while($statement->fetch())
			{
				// set the match id
				$this->id[] = $id;

				// set the challenger
				$this->challenger[] = $challenger;

				// set the defender
				$this->defender[] = $defender;
			}

			// so matches were found so return true
        	return true;
		}

        // the query failed so return false
        return false;
	}

	/**
	* unrolls the matches from the database
	* @id id match id to unroll
	*/
	public function unRoll($id)
	{
		// prepare the sql statement
		$statement = $this->db->prepare('DELETE FROM ' . TABLE_MATCHESINDEX . ' WHERE id = ? LIMIT 1');

		// bing the variables
		$statement->bind_param('i', $id);

		// if the statement executed successfully
		if ($statement->execute())
		{
			// get the number of results
			$statement->bind_result($row);

			// fetch the results
			$statement->fetch();

			// if the match was removed successfully
			if($row)
			{
				// return true
				return true;
			}

			// otherwise return false
			return false;
        }
	}

}

?>

Code: Select all

<?php

// disable display or error messages and log them instead
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On');
ini_set('log_errors', 'On');
ini_set('error_log', '/logs/error_log');

// include smarty library
require('libs/Smarty.class.php');

// include database library
require_once('libs/Database.class.php');

// include user library
require_once('libs/User.class.php');

// include user library
require_once('libs/Match.class.php');

// create instance of database class
$database = new Database();

// create instance of match class
$matches = new Match($database);

// create instance of smarty library
$smarty = new Smarty();

// set content header
header("Content-Type: " . USER_CONTENT_TYPE);

// start session
session_start();

// if the user isn't logged in
 if(!isset($_SESSION['username'], $_SESSION['password'])) {
 	// redirect to the login form
 	header("Location: index.php");
 }
// otherwise if the form hasn't been submitted
else
{
	if($matches->CheckPendingMatches($_SESSION['username']) == 0)
	{
		// set the dialog title
		$smarty->assign('title', 'Info');

		// set the dialog message stating there are no pending matches
		$smarty->assign('message', 'There are no pending matches');

		// set the back url
		$smarty->assign('backurl', 'showchallenges.php');

		// display it
		$smarty->display('messagedialog.tpl');
	}
	else
	{
		$rows = $matches->CheckPendingMatches($_SESSION['username']);
		echo "rows = $rows<br />";

		if($matches->getInfo($_SESSION['username']))
		{
			// set the challenger field
			$smarty->assign('challengers', $matches->getChallengers());

			// set the defender field'
			$smarty->assign('defenders', $matches->getDefenders());

			// set the match id
			$smarty->assign('ids', '1');

			// display it
			$smarty->display('viewchallenges.tpl');
		}
		// set the dialog title
		//$smarty->assign('title', 'Error');

		// set the dialog message stating the feature isn't implemented'
		//$smarty->assign('message', 'This feature isn\'t implemented yet');

		// set the back url
		//$smarty->assign('backurl', 'main.php');

		// display it
		//$smarty->display('messagedialog.tpl');
	}
}

?>
Screenie:
http://adobe.ly/PaUFoc as you can see it's returning strange results, more then 2 and they aren't even correct.The database data is
id, challenger, defender, status
1, admin, logster897, 0
2, pineapplelime, admin, 0
Any assistance in this matter would be greatly appreciated.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Returning 1 result

Post by Jackolantern »

That is a lot of code to pour over and understand (although I must say, you are structuring it nicely and I love the use of phpDoc). Have you nailed down the query that is causing the problem, and if so, which one is it?
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Returning 1 result

Post by Winawer »

The screenshot link doesn't work.
Aleeious
Posts: 55
Joined: Tue Sep 13, 2011 1:22 pm

Re: Returning 1 result

Post by Aleeious »

Winawer wrote:The screenshot link doesn't work.
Not sure why, but this one works.
Jackolantern wrote:That is a lot of code to pour over and understand (although I must say, you are structuring it nicely and I love the use of phpDoc). Have you nailed down the query that is causing the problem, and if so, which one is it?
Sorry and thanks. Yes I managed to fix it return so it only returns the 2 results, however it seems the resulting data is corrupted or something as its returning 5 data sets with data mixed from the 2 rows. If you need help understanding read my table data i posted in the first post and the updated screenshots. As far as code goes, i can shorten it to:

Code: Select all

    <?php

    class Match
    {
       // constant for open Match
       const STATUS_OPEN = 0;

       // constant for completed Match
       const STATUS_COMPLETED = 1;

       // constant for cancelled Match
       const STATUS_CANCELLED = 2;

       // constant for match that was result of error
       const STATUS_ERROR = 3;

       // match id
       private $matchID = array();

       // challenger of match
       private $challenger = array();

       // defender of match
       private $defender = array();

       // instance of database
       protected $db;

       /**
       * class constructor
       * @param $db database class instance
       */
        public function __construct(Database $db)
        {
            // get instance of database
            $this->db = $db;
        }

       /**
       * returns match id array
       * @return match id array
       */
       public function getID()
       {
          return $this->matchID;
       }

       /**
       * returns challenger array
       * @return challenger array
       */
       public function getChallengers()
       {
          return $this->challenger;
       }

          /**
       * returns defender array
       * @return defender array
       */
       public function getDefenders()
       {
          return $this->defender;
       }

        /**
       * counts the number of pending matches a player has
       * @param $username username to check number of pending challenges
       */
       public function CheckPendingMatches($username)
       {
          // prepare the sql statement to change the password
            $statement = $this->db->prepare("SELECT id FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? OR defender = ? LIMIT 5");

          // convert constant to variable
          $status = self::STATUS_OPEN;

            // bind the variables
            $statement->bind_param('ssi', $username, $username, $status);

            // if the statement executed successfully
            if ($statement->execute())
            {
                // store the result
                $statement->store_result();

                // get the number of results
                $totalPendingMatches = $statement->num_rows;

                // return the total number of pending matches
                return $totalPendingMatches;
            }

            // query didn't execute so return false
            return false;
       }

       /**
       * get the current matches the playerhas pending
       * @username username to check for matches for
       */
       public function getInfo($username)
       {
          // prepare the sql statement to change the password
            $statement = $this->db->prepare("SELECT id, challenger, defender FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? or defender = ? LIMIT 5");

          // convert constant to variable
          $status = self::STATUS_OPEN;

            // bing the variables
            $statement->bind_param('ssi', $username, $username, $status);

            // if the statement executed successfully
            if ($statement->execute())
            {
               // get the number of results
             $statement->bind_result($id, $challenger, $defender);

             // fetch all the results
             while($statement->fetch())
             {
                // set the match id
                $this->id[] = $id;

                // set the challenger
                $this->challenger[] = $challenger;

                // set the defender
                $this->defender[] = $defender;
             }

             // so matches were found so return true
               return true;
          }

            // the query failed so return false
            return false;
       }

    }

    ?>
and

Code: Select all

    <?php

    // disable display or error messages and log them instead
    ini_set('error_reporting', E_ALL | E_STRICT);
    ini_set('display_errors', 'On');
    ini_set('log_errors', 'On');
    ini_set('error_log', '/logs/error_log');

    // include smarty library
    require('libs/Smarty.class.php');

    // include database library
    require_once('libs/Database.class.php');

    // include user library
    require_once('libs/User.class.php');

    // include user library
    require_once('libs/Match.class.php');

    // create instance of database class
    $database = new Database();

    // create instance of match class
    $matches = new Match($database);

    // create instance of smarty library
    $smarty = new Smarty();

    // set content header
    header("Content-Type: " . USER_CONTENT_TYPE);

    // start session
    session_start();

    // if the user isn't logged in
     if(!isset($_SESSION['username'], $_SESSION['password'])) {
        // redirect to the login form
        header("Location: index.php");
     }
    // otherwise if the form hasn't been submitted
    else
    {
       if($matches->CheckPendingMatches($_SESSION['username']) == 0)
       {
          // set the dialog title
          $smarty->assign('title', 'Info');

          // set the dialog message stating there are no pending matches
          $smarty->assign('message', 'There are no pending matches');

          // set the back url
          $smarty->assign('backurl', 'showchallenges.php');

          // display it
          $smarty->display('messagedialog.tpl');
       }
       else
       {
          $rows = $matches->CheckPendingMatches($_SESSION['username']);
          echo "rows = $rows<br />";

          if($matches->getInfo($_SESSION['username']))
          {
             // set the challenger field
             $smarty->assign('challengers', $matches->getChallengers());

             // set the defender field'
             $smarty->assign('defenders', $matches->getDefenders());

             // set the match id
             $smarty->assign('ids', '1');

             // display it
             $smarty->display('viewchallenges.tpl');
          }
          // set the dialog title
          //$smarty->assign('title', 'Error');

          // set the dialog message stating the feature isn't implemented'
          //$smarty->assign('message', 'This feature isn\'t implemented yet');

          // set the back url
          //$smarty->assign('backurl', 'main.php');

          // display it
          //$smarty->display('messagedialog.tpl');
       }
    }

    ?>
The problem function possibly being getInfo(). Any assistance in this matter would be greatly appreciated.

Sincerely,

Aleeious
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Returning 1 result

Post by MikuzA »

Code: Select all

$statement = $this->db->prepare("SELECT id, challenger, defender FROM " . TABLE_MATCHESINDEX . " WHERE status = ? AND challenger = ? or defender = ? LIMIT 5");

$statement->bind_param('ssi', $username, $username, $status);
Shouldn't the binding go, 'iss', $status, $username, $username?
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
Post Reply

Return to “Beginner Help and Support”