The Boolean Error [SOLVED]

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
User avatar
RogueTomboy
Posts: 61
Joined: Sun May 22, 2011 3:42 pm

The Boolean Error [SOLVED]

Post by RogueTomboy »

This is driving me crazy and I don't know why I'm getting it...I mean, I get that it's saying it's a boolean error - which means a false return but when I run it through the MYSQL console they say it's my syntax.

I do have a debugging code on the top of my page but that doesn't show anything due to it being a logic error (as I was told :) )

I am logging in with the playerid of 1 from the database image below so I am using a user that has a character.

My table from Database
Image

ERROR from my Apache Log
PHP Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\\wamp\\www\\characterselected.php on line 26

ERROR that shows on my page
There has been an error during your login. Character not found for your account. Please log in again.


Errored Line

Code: Select all

//check to make sure that character id actually belongs to player
					$result = mysqli_fetch_assoc(mysqli_query($db, "SELECT cname FROM playercharacter WHERE playercharacterid='$playercharacterid' and playerid='$playerid'"));
Page Code

Code: Select all

<?php
$debug = true; // switch to false once you finished your game :)
define("DEBUG", $debug); // define a global usable DEBUG constant ( you can use it on all your pages after it is defined )
error_reporting(E_ALL); // tells PHP to report ALL errors possible ( notices, warnings, fatal errors et cetera )
ini_set('display_errors', $debug); // this command actually forces PHP to display them, what good is reporting if they`re not going to be displayed?
?>

<?php
	include 'connect.php';
	session_start();
	$exitScript = 0;
	$username = $_SESSION['name'];
	if(!$username) {
                       echo "could not get username";
	               exit;
	}
	$playerid = $_SESSION['playerid'];
	if(!$playerid) {
                       echo "could not get playerid";
	               exit;
	}
        $playercharacterid = $_GET['pcid'];
        if(!$playercharacterid) {
                       echo "could not get playercharacterid";
	               exit;
	       }
	if (!$_SESSION['name']) {
		$exitScript = 1;
	}

?>

			<?php 
				if ($exitScript == 0){

					//check to make sure that character id actually belongs to player
					$result = mysqli_fetch_assoc(mysqli_query($db, "SELECT cname FROM playercharacter WHERE playercharacterid='$playercharacterid' and playerid='$playerid'"));

 					if (!$result) {
						echo "There has been an error during your login. Character not found for your account. Please log in again.<br /><br />";
						$_SESSION = array();
						echo "<a href='index.php'>Log in</a>";
						exit();
					}

					//process successful player/character login
					if (!isset($_SESSION['playercharacterid'])) {
						$_SESSION = $playercharacterid;
					}

					echo "You have successfully logged in!<br /><br />";
					echo "<a href='index.php'>Welcome to Neohazard!</a>";
					
				} else {
					echo "You are not logged in. Please go back and log in.<br />";
					echo "<a href='index.php'>Home Page</a>";
				}
			?>
		</div>
Last edited by RogueTomboy on Mon Jul 25, 2011 7:16 am, edited 1 time in total.
Need art? Commissions are always open!
http://www.ladytankstudios.com

Currently addicted to:
Image
Collect & Explore for pets
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: The Boolean Error.

Post by Jackolantern »

While I don't know if this is the problem, this line is a serious problem:

Code: Select all

$_SESSION = $playercharacterid;
What it would do as written is overwrite the entire SESSION array with the value stored in $playercharacterid. This is the same as deleting the SESSION array, which can have far-reaching consequences. You would need to change it to something like this:

Code: Select all

$_SESSION['pcid'] = $playercharacterid;
And I didn't know you were having the error from mysqli_fetch_assoc(), but it does appear to be a logic error somewhere. The fact that a Boolean value is being sent in means FALSE was returned from mysqli_query() as it did not fetch any results.
The indelible lord of tl;dr
User avatar
RogueTomboy
Posts: 61
Joined: Sun May 22, 2011 3:42 pm

Re: The Boolean Error.

Post by RogueTomboy »

I've been stuck on it for days. A few folks said to change my $db in that line to the end of my query - which made it blow up big time (I knew it would but it was the one thing I hadn't tried) - and I don't know what's wrong. I've echoed out the beginning of the variables under my session_start - unless I did that wrong - and I still got the same error.

So what am I doing wrong? I want a user to be able to log in with their username (that works) and password (that works), pick a character from their list (that works), and then to enter the game - in a session - with that character to play and do as that character.

Something isn't connecting with my DB and I don't know what it is.
Need art? Commissions are always open!
http://www.ladytankstudios.com

Currently addicted to:
Image
Collect & Explore for pets
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: The Boolean Error.

Post by Xaleph »

Aye, logic errors are common too, too bad there`s no real argument to be passed there.. Anyway, as I`m loading up my heavy frikkin editor, I wanted to comment on Jack. I can`t see the code you are talking about? All iI noticed was this: $_SESSION = array(); which really all it does is reset $_SESSION as an array. Nothing bad there, mostly because it`s in the else clause and no user is allowed to login.

O and this, the mysqli_query() will return false if nothing was selected. A simple check like:

if(!($return = mysqli_query($sql))){
echo 'Nothing found... :(';
}else{
$result = mysqli_fetch_assoc($return);
}

Also, and this helps me for some reason, echo out/print out the query itself. Like echo "SELECT * FROM sometable WHERE somefield = '$somevar'";
Then run that exact query in your admin to see if it works.

as an edit and addition to your PM:

remove the ' single quotes from the query. ID`s are of type INT ( integer ). This means you cannot send a string to pass for a number right? Removing the quotes should fix the problem.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: The Boolean Error.

Post by Callan S. »

Just checking this

Code: Select all

$playercharacterid = $_GET['pcid'];
So your passing the player id through the address line thing?

When your running the page it's being passed the id? Like "?pcid=1"

You probably are, but sometimes you just have to check the obvious! :)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: The Boolean Error.

Post by Jackolantern »

Xaleph wrote:Anyway, as I`m loading up my heavy frikkin editor, I wanted to comment on Jack. I can`t see the code you are talking about? All iI noticed was this: $_SESSION = array(); which really all it does is reset $_SESSION as an array
It is down near the bottom, which is why I was thinking it probably wasn't the error causing the problem since I believe the error is happening earlier on in the script. Yes, $_SESSION = array() will only reset it, but setting $_SESSION equal to a scalar variable, like $characterid or something like that, will effectively kill sessions and can, with some PHP or framework features turned on that require sessions, screw up tons of things :o
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: The Boolean Error.

Post by Xaleph »

Oh i totally agree.. We`ve all been there lol.. The easiest way to kill a session is simple and effective. session_destroy(); Kills it.

Anyway, i believe the problem should be solved any time soon.
User avatar
RogueTomboy
Posts: 61
Joined: Sun May 22, 2011 3:42 pm

Re: The Boolean Error.

Post by RogueTomboy »

Yes Xaleph is working hard to help me figure it out!
Need art? Commissions are always open!
http://www.ladytankstudios.com

Currently addicted to:
Image
Collect & Explore for pets
Post Reply

Return to “Beginner Help and Support”