Guest Posted November 24, 2011 Share Posted November 24, 2011 Is there a function which will allow me to throw the values into an array and post them into the DB as separate records? If so a little help would be appreciated! if(empty($row)) { //Need a better way. $sql = "INSERT INTO phpbb_user_items VALUES (13, '$user_id', '0'), (12, '$user_id', '0'), (11, '$user_id', '0'), (9, '$user_id', '0'), (27, '$user_id', '0'), (15, '$user_id', '0'), (16, '$user_id', '0'), (22, '$user_id', '0'), (21, '$user_id', '0'), (23, '$user_id', '0'), (24, '$user_id', '0'), (26, '$user_id', '0'), (29, '$user_id', '0'), (30, '$user_id', '0'), (31, '$user_id', '0'), (32, '$user_id', '0'), (33, '$user_id', '0') "; $result = $db->sql_query($sql); } Link to comment https://forums.phpfreaks.com/topic/251701-i-need-an-alternative-way-to-do-this-query/ Share on other sites More sharing options...
teynon Posted November 24, 2011 Share Posted November 24, 2011 There sure is! Here it is: function totallyAwesomeFunctionForTonyhhInsertQueries($tableName, $values, $keys = array()) { $insert = "INSERT INTO `{$tableName}`"; if (count($keys) > 0) { $insert .= "`". implode("`,`", $keys) ."`"; } $insert .= " VALUES"; foreach ($values as $key => $value) { $insert. = "'". implode("', '", $value) ."'"; } return $insert; } And if you want to insert the same value again and again, there is another function for that! function totallyAwesomeFunctionForTonyhhInsertQueryRepeat($query, $times) { for ($i = 0; $i < $times; $i++) { mysql_query($query); } } I tickle myself sometimes... Anyways, point is, you can make functions... Link to comment https://forums.phpfreaks.com/topic/251701-i-need-an-alternative-way-to-do-this-query/#findComment-1290853 Share on other sites More sharing options...
Guest Posted November 24, 2011 Share Posted November 24, 2011 There sure is! Here it is: function totallyAwesomeFunctionForTonyhhInsertQueries($tableName, $values, $keys = array()) { $insert = "INSERT INTO `{$tableName}`"; if (count($keys) > 0) { $insert .= "`". implode("`,`", $keys) ."`"; } $insert .= " VALUES"; foreach ($values as $key => $value) { $insert. = "'". implode("', '", $value) ."'"; } return $insert; } And if you want to insert the same value again and again, there is another function for that! function totallyAwesomeFunctionForTonyhhInsertQueryRepeat($query, $times) { for ($i = 0; $i < $times; $i++) { mysql_query($query); } } I tickle myself sometimes... Anyways, point is, you can make functions... Well yeah, I just didn't know how to approach it, but thanks! (Keeping the function name) Link to comment https://forums.phpfreaks.com/topic/251701-i-need-an-alternative-way-to-do-this-query/#findComment-1290860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.