Mutley Posted May 22, 2007 Share Posted May 22, 2007 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 More sharing options...
per1os Posted May 22, 2007 Share Posted May 22, 2007 <?php $result = mysql_query("SELECT `damage`, `slot` FROM `own` WHERE user_id = '$player1' AND loadout = '1' ORDER BY `slot`;"); while($row = mysql_fetch_array( $result )) { $damage[$row['slot']] = $row; } ?> Maybe ??? Link to comment https://forums.phpfreaks.com/topic/52533-quicker-way-of-doing-this/#findComment-259225 Share on other sites More sharing options...
valtido Posted May 22, 2007 Share Posted May 22, 2007 i would of used a loop like for or while www.w3schools.com/php should have some info there lool Link to comment https://forums.phpfreaks.com/topic/52533-quicker-way-of-doing-this/#findComment-259226 Share on other sites More sharing options...
Wildbug Posted May 22, 2007 Share Posted May 22, 2007 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' Link to comment https://forums.phpfreaks.com/topic/52533-quicker-way-of-doing-this/#findComment-259227 Share on other sites More sharing options...
Mutley Posted May 22, 2007 Author Share Posted May 22, 2007 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? Link to comment https://forums.phpfreaks.com/topic/52533-quicker-way-of-doing-this/#findComment-259246 Share on other sites More sharing options...
Wildbug Posted May 22, 2007 Share Posted May 22, 2007 Instead of having lots of OR statments, the expression "slot IN ('car','1','2','3','4','5')" is true if the value in "slot" is any of those in the parenthetical list. If you need to know which one matched, return it in the SELECT list, too. Link to comment https://forums.phpfreaks.com/topic/52533-quicker-way-of-doing-this/#findComment-259263 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.