Rows Not Existing

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Rows Not Existing

Post by Epiales »

Okay... Ran into a little snag here.

First, let's break everything down. I have, thus far, bus, taxi, car -- Must have ONE of these to travel from city to city. Each one have their own table in the database:

Bus:

Code: Select all

$sql = "SELECT * from bususerbdlg WHERE playerid ='$_SESSION[userid]' LIMIT 1";
Taxi:

Code: Select all

$sql = "SELECT * from taxiuserbdlg WHERE playerid ='$_SESSION[userid]' LIMIT 1";
Car:

Code: Select all

$sql = "SELECT * from caruserbdlg WHERE playerid ='$_SESSION[userid]' LIMIT 1";

Problem is, is I need to be able to verify that each user has at least ONE of the above in order for them to travel to the next location. Now, if the user has already click on one of the above and started the countdown timer to build each one, then there is no problem, as the row is inserted into the table, so the error messages work just fine. It's BEFORE they click on one to build them, is where the problem is.

If I put this, and the rows are there, it works:

Code: Select all

if ($owned_bus != 1) {
    $error_mess = "<span id='errormess'><big><left><font color='red'><b>You must first have transportation to travel here!</b></center></span></big></font>";
			}


if ($owned_taxi != 1) {
    $error_mess = "<span id='errormess'><big><left><font color='red'><b>You must first have transportation to travel here!</b></center></span></big></font>";
			}
But it's when nothing has been built. There are no rows in the database yet with their user id. And since each of these are in different tables, that is my problem.

What I need is either a way to rewrite these to combine them into one table, or a snippet of code that will check each table to check and notice a row is not yet in the table with their user id.... It needs to check if they have one of the above AND check that a row does exist or not, or they can't travel.

Thanks!
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Rows Not Existing

Post by Epiales »

Any ideas? :cry: :cry:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Rows Not Existing

Post by Jackolantern »

Check out this SO question. I actually like the 2nd answer a bit better (but it doesn't return the record found, so that may not fit your needs; it only returns true or false whether it was existed in one of them). But regardless, there are several ways to do it all listed on there. If you need the record returned, it will use a join, to add all of the tables together, and then select the record wherever it was found in the tables in one query. If you go that route, you would know you need to show the error if no results are returned.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Rows Not Existing

Post by Epiales »

Jackolantern wrote:Check out this SO question. I actually like the 2nd answer a bit better (but it doesn't return the record found, so that may not fit your needs; it only returns true or false whether it was existed in one of them). But regardless, there are several ways to do it all listed on there. If you need the record returned, it will use a join, to add all of the tables together, and then select the record wherever it was found in the tables in one query. If you go that route, you would know you need to show the error if no results are returned.
Not sure what I'm looking at? It sees if a rows in there, it's when there's no rows there... That's my situation. I can join the tables with no problem if need be, but a row has to be there for it to throw the errormessage "you must have one of these to travel" ect.... It's that if they have not built one, then there is no table and thus it throws an undefined index/object and it doesn't work right...
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Rows Not Existing

Post by Jackolantern »

Just check if no results were returned. If you combine it all into one query using the link I gave above, you can then easily check if you did not get a result, meaning the player has not built any of the required buildings. If you get that mysql_num_rows or mysqli_num_rows (depending on which you are using) is equal to 0, there is no building, and you can show the error message.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Rows Not Existing

Post by Epiales »

Jackolantern wrote:Just check if no results were returned. If you combine it all into one query using the link I gave above, you can then easily check if you did not get a result, meaning the player has not built any of the required buildings. If you get that mysql_num_rows or mysqli_num_rows (depending on which you are using) is equal to 0, there is no building, and you can show the error message.
Yeah, I'm finding out that inner joining three tables is being more difficult than I expected :mrgreen: :mrgreen:

Code: Select all

bususerbdlg

caruserbdlg

taxiuserbdlg
They all have the fields playerid and owned....

I need it combined to check for the field owned = 1.... If they own one then they can travel...if not then they can't.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Rows Not Existing

Post by Epiales »

Is this even remotely close? LOL

Code: Select all

$query = "SELECT bus.owned as bus_owned, car.owned as car_owned, taxi.owned as taxi_owned from bususerbdlg bus inner join caruserbdlg car ON bus.owned = car.owned INNER JOIN taxiuserbdlg taxi on bus.owned = taxi.owned";
And how would I check to see if the playerid was equal to the session userid? grrrr....
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Rows Not Existing

Post by Epiales »

Maybe something like this:

Code: Select all

$sql = "SELECT bus.playerid as playerid, car.playerid as playerid, taxi.playerid as playerid from bususerbdlg INNER JOIN 
caruserbdlg ON bus.playerid = car.playerid INNER JOIN taxiuserbdlg on bus.playerid= taxi.playerid WHERE bus.playerid = 
$_SESSION[userid] AND taxi.playerid = $_SESSION[userid] AND car.playerid = $_SESSION[userid]";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);

if($owned_bus != 1 || $owned_taxi !=1) {

echo "You need to build one";

}
I really don't have a clue, as this doesn't work lol :oops:

I get this error:

Code: Select all

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login3\map.php on line 759
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Rows Not Existing

Post by Winawer »

Is there a difference between a bus, a taxi and a car or are they all the same from a gameplay point of view?
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Rows Not Existing

Post by Epiales »

Winawer wrote:Is there a difference between a bus, a taxi and a car or are they all the same from a gameplay point of view?
They are identical in the tables and fields...but one must have at least one of them in order to travel to another city. Doesn't matter which one. The only difference is the time it takes to build one.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Advanced Help and Support”