Jump to content

I need an alternative way to do this query


Spring

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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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.