Jump to content

[SOLVED] Is this bad coding?


deadlyp99

Recommended Posts

So, I remember once reading that nesting sql queries was bad, and I can see that it could be. But I'm not quite sure how I can make my code better, but it is already getting messy at the start of a long day of coding.

 

I am going to post my table structure, along with the rows being using in the particular query (for data example).

 

Now, I've been coding for a couple years, and I truly do want to improve in my efficiancy and code legibility. However I'm already starting to see this is going to turn into a big confusing mess if I don't get help making it better.

 

Thanks in advance for any replies, and have a good day

 

 $row `UsersGeneral`
ID 	Username 	PassHash 	   Email 	  LandLordID  	RaceType 	Active 	LastLogin 	LastIP 	ActivateID 	RegisterIP
17 	nocare 	    xxxxx 	xxxx@gmail.com 	17 	      2 	      1 	     date 	xxx.xxx.xxx.xxx 	xxxxx 	xxx.xxx.xxx.xxx

$row2 `LandLord`
LandLordID	SquareID 	Capitol
17 	         9033 	    1

 

$row3 `MapSquares`
SquareID 	X 	Y 	LandLordID  	MapType 	OasisType 	CityID
  9033 	  39 	-7 	    17 	         1          	0 	  1

 

		$name = $_SESSION['name']; // <-- nocare
		$User = "SELECT * FROM `UsersGeneral` WHERE Username='$name' LIMIT 1";
		$UserQuery = mysql_query($User) or die(mysql_error());
		while ($row = mysql_fetch_array($UserQuery))
		{
			$LandLordID = $row['LandLordID'];
    				$UserCapitol = "SELECT * FROM `LandLord` WHERE LandLordID='$LandLordID' LIMIT 1";
    				$UserCapitolQuery = mysql_query($UserCapitol) or die(mysql_error());
    				while ($row2 = mysql_fetch_array($UserCapitolQuery))
    				{
				$CapitolSquare = "SELECT * FROM `MapSquares` WHERE LandLordID='$LandLordID' LIMIT 1";
				$CapitolSquareQuery = mysql_query($CapitolSquare) or die(mysql_error());
				while ($row3 = mysql_fetch_array($CapitolSquareQuery))
				{
					echo "Username: ".$row['Username']." ID: ".$row['ID']." LandLordID: ".$row['LandLordID']." SquareID: ".$row2['SquareID']." Capitol: ".$row2['Capitol']." X: ".$row3['X']." Y: ".$row3['Y']." MapType: ".$row3['MapType']." CityID:".$row3['CityID'];
				}
    				}
		}
		mysql_close($conn);

 

Output: Username: nocare ID: 17 LandLordID: 17 SquareID: 9033 Capitol: 1 X: 39 Y: -7 MapType: 1 CityID:1

Link to comment
Share on other sites

if landordid in the landlord table is the primary key you don't need the limit 1. Also if the landlord can only have 1 capitalsquare    you can do a unique key on the landlordid and capitalsquareid.    Then you can do a join

SELECT * UsersGeneral JOIN LandLord on UsersGeneral.LandLordID=LandLord.LandLordID JOIN  MapSquares on LandLord.LandLordID=CapitalSquares.LandLordID WHERE UsersGeneral.Username='$name'

 

Link to comment
Share on other sites

Thank you for replying taquitosensei, I just about quadrupled my code since I posted, and while it doesn't effect the mysql, its going to be a little while before I get a chance to implement this query to test it out.

 

So if I am undererstanding correctly, I'd go about using this like:

 

$sql = "SELECT * UsersGeneral JOIN LandLord on UsersGeneral.LandLordID=LandLord.LandLordID JOIN  MapSquares on LandLord.LandLordID=CapitalSquares.LandLordID WHERE UsersGeneral.Username='$name'";
$result = mysql_query($User) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{ 
      //Do stuff by Column name
}

Link to comment
Share on other sites

Wonderful wonderful :D

I got it working. Thanks to you I shortened my code a lot

Thanks a lot

 

From:

		$name = $_SESSION['name'];
		$User = "SELECT * FROM `UsersGeneral` WHERE Username='$name' LIMIT 1";
		$UserQuery = mysql_query($User) or die(mysql_error());
		while ($row = mysql_fetch_array($UserQuery))
		{
			$LandLordID = $row['LandLordID'];
    				$UserCapitol = "SELECT * FROM `LandLord` WHERE LandLordID='$LandLordID' LIMIT 1";
    				$UserCapitolQuery = mysql_query($UserCapitol) or die(mysql_error());
    				while ($row2 = mysql_fetch_array($UserCapitolQuery))
    				{
				$CapitolSquare = "SELECT * FROM `MapSquares` WHERE LandLordID='$LandLordID' LIMIT 1";
				$CapitolSquareQuery = mysql_query($CapitolSquare) or die(mysql_error());
				while ($row3 = mysql_fetch_array($CapitolSquareQuery))
				{
                                        }
                                 }
                           }

 

To:

 

$name = $_SESSION['name'];
		$sql = " SELECT * FROM UsersGeneral JOIN LandLord ON UsersGeneral.LandLordID = LandLord.LandLordID JOIN MapSquares ON LandLord.LandLordID = MapSquares.LandLordID WHERE UsersGeneral.Username = '$name'";
		$result = mysql_query($sql) or die(mysql_error());
		while ($row = mysql_fetch_array($result))
		{
                         }

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.