Jump to content

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?

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.