Jump to content

I need an alternative way to do this query


Guest

Recommended Posts

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);
	}

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

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)

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.