Jump to content
MakeWebGames

Too many Queries


Damond

Recommended Posts

tl;dr; :p

 

Lets say you run $r in a loop but $r returns false, how would you handle that? This is why it is a bit better in most cases to query your database then fetch the results seperately:

//Query our db
$q = $db->query("SELECT * FROM users WHERE userid=$userid");
//Are there any rows from our query?
if($db->num_rows($q)) {
//Yes, lets loop through them
   $results = array();
   while($r = $db->fetch_row($q)) {
       $results[] = $r;
   }
   echo $results."\n\r";
}
//Nope, no rows lets inform them
else {
   echo "No results found!";
}

 

Opposed to:

$r = $db->fetch_row($db->query("SELECT * FROM users WHERE userid=$userid"));
results = array();
while($r) {
   $results[] = $r;
} 
echo $results."\n\r";

 

In all honesty I think the first one looks much cleaner and easier to read even though its more than twice the lines but you have more control over the output much easier than stacking the methods together

Ok now I get what you mean, and that makes perfect sense. Thanks for explaining it better :)

Link to comment
Share on other sites

There's nothing wrong with having an extra line of code in your file? What difference does having 3 extra lines of code make?

From your mentality of being able to do something in one line of code, you could do your while loop like this:

 

while($r=$db->fetch_row($db->query("SELECT * FROM users"))){if($r['userid']==$userid){echo("We found you!");}}

i one lined my whole while loop.

It's ugly as hell, and if something goes wrong I have to look through that full one line to try find my problem. PHP might tell me my error is in line 46, but if I have 8 different things going on in line 46, I have to look at all 8 to try and see where my problem is coming.

Readability is a developers best friend. If you can scroll through your code and have everything happen line by line, you'll know exactly what is happening in your file and why it is happening. It makes it easy to edit and fix if something goes wrong.

As far as I'm aware extra lines of code have never made a speed difference to actually loading a page and if it does its negligible because users won't notice it.

Also as Kyle says, you need to make sure there is data to loop through. Your one liner as my mine, doesn't give the opportunity to do that effectively in my opinion. Yeah you can use a try and catch block, but I personalu believe that checking if there are rows matching your sql query is a better way of doing it.

Edited by Coly010
Link to comment
Share on other sites

Finding ways to minimise code length is only really useful in friendly competitions. This isn't 1990 and we no longer run high end servers on 256mb of memory, so guess what that means? We don't need to micro optimise, thankfully.

Why thankfully? We can no write code that humans can read too, which means programming is no longer authoring books that can only be read by servers.

Heres my approach to this.

More lines than most posted here... but it's easy to read, so in the future, maintaining and updating will be a breeze.

 

<?php
// Initial vars; No point checking for the else statement
// if it exists, it will over-write;
$on_hand  = 'None';
$on_hand2 = 'None';
$on_hand3 = 'None';
$on_hand4 = 'None';
$on_hand5 = 'None';
$on_hand6 = 'None';


// Create array - why do these run in 3s?
$items  = '84, 87, 90, 93, 96, 99';


// Create query;
$sql    = "SELECT * FROM inventory WHERE inv_userid = " . $ir['userid'] . " AND inv_itemid IN (" . $items . ");";


// Query for results;
$result = $db->query($sql);


// Check it returns some data - why bother attempting to process the data if no results? 
if ($db->num_rows($result)) {
// Loop results, since they exist;
while ($row = $db->fetch_row($result)) {
	// Check item id is not zero or blank; and has a quantity
	if ($row['inv_itemid'] != 0 && $row['inv_itemid'] != "" && $row['inv_qty'] > 0) {
		// Passed all checks? Assign the values;
		switch ($row['inv_itemid']) {
			case "84": 		$on_hand  = $inv['qty']; 	break;
			case "87": 		$on_hand2 = $inv['qty']; 	break;
			case "90": 		$on_hand3 = $inv['qty']; 	break;
			case "93": 		$on_hand4 = $inv['qty']; 	break;
			case "96": 		$on_hand5 = $inv['qty']; 	break;
			case "99": 		$on_hand6 = $inv['qty']; 	break;
			default:		/*do nothing*/ echo ""; 	break;
		}
	}
}
}

 

It's all well and good cutting lines, if you are actually cutting waste - doing it just to try and look smart will only make you look st00p1dz. (No more so than writing "st00p1dz" though) #nuffSaid.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...