Returns Wrong Defender

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

Returns Wrong Defender

Post by Aleeious »

I am trying to update a column in a predetermined row. However when i put in any id it returns the wrong defender from the row. For example :
id challenger defender status
1 admin logster897 0
2 pineapplelime admin 0
3 admin testuser 0
If i ask it to resurn the defender of match id 1 it returns blank. I have narrorrowed it down to the bit of code below. I can confirm the statement is executed, binded and fetched so the query is working. I tried to use the query manually in phpmyadmin and the query returns the correct result so it seems to be something with my code.

Code: Select all

/**
	* get the participants of a match
	* @param $id id of match to check participants for
	*/
    public function getHomeParticipants($id, $status, $username)
    {
        // prepare the sql statement to change the password
        $statement = $this->db->prepare("SELECT defender FROM " . TABLE_MATCHESINDEX . " WHERE id = ? AND status = ? AND challenger = ? LIMIT 1");

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

        // if the statement executed successfully
        if ($statement->execute())
        {
            echo "executed<br />";
        	// get the number of results
			$statement->bind_result($defender);
            echo "binded result<br />";
			// fetch all the results
			$statement->fetch();
            echo "fetched<br />";
            // set the defender
            $this->defender[] = $defender;
            echo "Defender = " . $defender . "<br />";
            // so matches were found so return true
        	return true;
		}
        // the query failed so return false
        return false;
    }
I can't seem to figure out why it's returning empty. Any assistance would be greatly appreciated.

Sincerely,

Aleeious
User avatar
kaos78414
Posts: 507
Joined: Thu Jul 22, 2010 5:36 am

Re: Returns Wrong Defender

Post by kaos78414 »

I don't see where $defender is coming from:

Code: Select all

$this->defender[] = $defender;
Shouldn't you be getting an undefined variable error of some kind?

Also, wherever $defender is coming from you seem to be setting it as the first index in the classes array so this:

Code: Select all

echo "Defender = " . $defender . "<br />";
should probably be:

Code: Select all

echo "Defender = " . $this->defender[0] . "<br />";
But without more context I can't really be sure what is going wrong. Maybe try wrapping everything in a try-catch block and echo the resulting exception to see if you can better spot the error:

Code: Select all

try {
    // your code here
} catch (\Exception $e) {
    var_dump($e->getMessage()); die();
}
w00t
Post Reply

Return to “Beginner Help and Support”