Jump to content

Quicker way of doing this...


Mutley

Recommended Posts

I have repetitive code, that selects data from an SQL table, then gives it a variable however it takes about 40 lines to do 5 different fields, here is my code:

 

 

		$result = mysql_query("SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot = 'car' AND loadout = '1' LIMIT 1");
	while($row = mysql_fetch_array( $result ))
    	{
	$damage1_car = $row['damage'];
	}
	$result = mysql_query("SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot = '1' AND loadout = '1' LIMIT 1");
	while($row = mysql_fetch_array( $result ))
    	{
	$damage1_1 = $row['damage'];
	}
	$result = mysql_query("SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot = '2' AND loadout = '1' LIMIT 1");
	while($row = mysql_fetch_array( $result ))
    	{
	$damage1_2 = $row['damage'];
	}
	$result = mysql_query("SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot = '3' AND loadout = '1' LIMIT 1");
	while($row = mysql_fetch_array( $result ))
    	{
	$damage1_3 = $row['damage'];
	}
	$result = mysql_query("SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot = '4' AND loadout = '1' LIMIT 1");
	while($row = mysql_fetch_array( $result ))
    	{
	$damage1_4 = $row['damage'];
	}
	$result = mysql_query("SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot = '5' AND loadout = '1' LIMIT 1");
	while($row = mysql_fetch_array( $result ))
    	{
	$damage1_5 = $row['damage'];
	}

 

 

I'm not sure what to do, I was thinking a function but not sure how to do this, as you can see only the variable and the "slot" changes, so not much.

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/52533-quicker-way-of-doing-this/
Share on other sites

Is the "LIMIT 1" necessary?  Are there multiple values otherwise?

 

If not:

SELECT `damage` FROM `own` WHERE user_id = '$player1' AND slot IN ('car','1','2','3','4','5') AND loadout = '1'

 

Nope it's not necessary anymore.

 

How would I use that, what does the "IN" statement do?

Archived

This topic is now archived and is closed to further replies.

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