Jump to content

[SOLVED] MySQL + PHP Problem


killah

Recommended Posts

Ok i have 2 table's.

 

Table 1 => house_rentals | Structure:

rID

rOWNER

rRENTER

rWILL

rHOUSE

rPRICE

rDAYSL

 

Table 2 => house_owned | Structure:

oID

oOWNER

oWILL

oUPGRADES

oUPKEEP

oRENTED

oIMAGE

 

Ok this is what i want to do.

 

I have a main page. Called property's. this is for a rpg game. I can buy multiple houses and then either move into them or lease them to some one else. I got all that done. But when i lease it to some one it does not show up in the propertys main page. I tried left join but just never worked.

 

This is my current query:

 

$my_houses = mysql_query
("
	SELECT oHOUSE,oWILL,oRENTED,oIMAGE,oUPGRADES,oID FROM `house_owned` WHERE `oOWNER` = ".$ir['userid']." OR `oRENTED` = ".$ir['userid']
) or die(mysql_error());

 

Can anyone modify that to fit?

Link to comment
Share on other sites

I now have this query:

 

$my_houses = mysql_query
("
	SELECT 
		o.oHOUSE,o.oWILL,o.oRENTED,o.oIMAGE,o.oUPGRADES,o.oID,o.oOWNER
	FROM 
		`house_owned` `o`
	LEFT JOIN
		`house_rentals` `r`
	ON
		`r`.`rHOUSE` = `o`.`oID`
	WHERE 
		`o`.`oOWNER` = ".$ir['userid']." 
	OR 
		`o`.`oRENTED` = ".$ir['userid']."
	OR
		`r`.`rRENTER` = ".$ir['userid']
) or die(mysql_error());

 

How ever. My problem now is that it's showing me having 2 house's. 1 because i am renting it. 2 because some one else own's it.

Link to comment
Share on other sites

When you do your join, you need to say on what field the tables join.

 

So your join will be something like:

 

      SELECT
         o.oHOUSE,o.oWILL,o.oRENTED,o.oIMAGE,o.oUPGRADES,o.oID,o.oOWNER
      FROM
         `house_owned` `o`
      LEFT JOIN
         `house_rentals` `r` on r.rOWNER = o.OWNER

 

I am not fully sure i understand what you are trying to achieve, but really you should specify how the tables are linked in a join.

Link to comment
Share on other sites

I tried

 

$my_houses = mysql_query
("
SELECT
	o.oHOUSE,o.oWILL,o.oRENTED,o.oIMAGE,o.oUPGRADES,o.oID,o.oOWNER
FROM
	`house_owned` `o`
INNER JOIN
	`house_rentals` `r`
ON
	`r`.`rOWNER` != `o`.`oOWNER`
WHERE
	`o`.`oOWNER` = ".$ir['userid']."
OR
	`o`.`oRENTED` = ".$ir['userid']."
AND
	`r`.`rHOUSE` = `o`.`oID`
") or die(mysql_error());

 

Just does not seem to work. Ive tried about 15+ way's now and not a single one work's.

Link to comment
Share on other sites

Why are you doing ` around every little thing? It just makes it weird and odd.

 

   $my_houses = mysql_query
   ("
   SELECT
      o.oHOUSE,o.oWILL,o.oRENTED,o.oIMAGE,o.oUPGRADES,o.oID,o.oOWNER
   FROM
      house_owned o
   INNER JOIN
      house_rentals r
   ON
      r.rOWNER <> o.oOWNER
   WHERE
      o.oOWNER = ".$ir['userid']."
   OR
      o.oRENTED = ".$ir['userid']."
   AND
      r.rHOUSE = o.oID
   ") or die(mysql_error());

 

If you want it to be != you need to use <> in MySQL.

 

See what that gets you.

Link to comment
Share on other sites

Hi. Well i used your code above and it showed me all the houses in the list but only it displayed all the houses that people are living in but infact they all appeared as me as the only. So basicly it was displaying 992 row's but all of the houses were mine.

 

I then moved to this code:

 

$my_houses = mysql_query
("
	SELECT
		o.oHOUSE,o.oWILL,o.oRENTED,o.oIMAGE,o.oUPGRADES,o.oID,o.oOWNER
	FROM
		house_owned o
	INNER JOIN
		house_rentals r
	ON
		r.rHOUSE = o.oID
	WHERE
		o.oOWNER = ".$ir['userid']."
	OR
		o.oRENTED = ".$ir['userid']."
	AND
		r.rOWNER <> o.oOWNER
	") or die(mysql_error());

 

That work's but i own 4 houses and am renting 1 house.

 

The 1 house i am renting is showing and only 2 out of the 4 houses are showing.

Link to comment
Share on other sites

Can you describe to me how it is setup? Does not need to be hugely indepth.

 

Here is what I am getting:

 

You have two tables, the houses you own and the houses you rent. You want to display all the houses that you own and rent, but you do not want to display the houses that you are renting to which you are the owner?

 

Now, I see the inner join, you are trying to join rented rHouse to oID, are these to values coinciding with each other? I take it they are, just had to verify. Essentially that join, is limiting your query. It will show only houses that you are renting because of that portion:

 

   $my_houses = mysql_query
   ("
      SELECT
         o.oHOUSE,o.oWILL,o.oRENTED,o.oIMAGE,o.oUPGRADES,o.oID,o.oOWNER
      FROM
         house_owned o
      WHERE
        ( o.oOWNER = ".$ir['userid']."
      OR
         o.oRENTED = ".$ir['userid'].")

      ") or die(mysql_error());

 

Now where I am getting confused, is why are you trying to access the house rented table, if you are not even using that in your select statement?

 

I think the query above is what you want. Give it a try and see.

Link to comment
Share on other sites

I do not know how u did it. But you did it. Yes that is my explanation of the query to what you are getting. The query you posted. Works. How ever i got to run more test's from other people's view to see if it fully work's.

 

Thank's so much premiso. If there were karma here i would definatly give you a +1 ;)

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.