Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

MSSQL 2005 - Add ID value to ID column when INSERTING

// @TableName is obviously the TABLE name u use
// @ColumnName is obviously the COLUMN name u use

---Get next ID number
	DECLARE 
		@ID int
	
	IF (SELECT count(*) FROM @TableName ) > 0
		BEGIN
			SELECT @ID  = max(ColumnName ) from @TableName
			SET @ID = @ID + 1 
		END
	ELSE
	BEGIN
		SET @ID  = 1
	END

PHP: Insert Data Into MySQL Table Using An Array

Inserts the values of an array into a table. Also supports specifying if a specific field needs to be encoded using PASSWORD()
Parameters:
Table: Name of table to insert into
Data: array of $field->$value of new data
Password Field: Which field in the data array needs to be surrounded with PASSWORD() (optional)

function mysql_insert_array($table, $data, $password_field = "") {
	foreach ($data as $field=>$value) {
		$fields[] = '`' . $field . '`';
		
		if ($field == $password_field) {
			$values[] = "PASSWORD('" . mysql_real_escape_string($value) . "')";
		} else {
			$values[] = "'" . mysql_real_escape_string($value) . "'";
		}
	}
	$field_list = join(',', $fields);
	$value_list = join(', ', $values);
	
	$query = "INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ")";
	
	return $query;
}

C#: Break Long Strings of Text That Don't Contain A Space

Long strings of text with no spaces breaks most tables since the browser doesn't know where to start a new line. This function breaks a string after a number of characters so that they can be properly displayed in tables

// Put this at the top
using System.Text.RegularExpressions;
//

public string BreakLongString(string SubjectString, int CharsToBreakAfter)
{
	string Pattern = "\\S{" + CharsToBreakAfter + ",}";
	int Counter = 0;
	bool IsMatching = Regex.IsMatch(SubjectString, Pattern);
	while (IsMatching)
	{

		Counter++;
		string MatchedString = Regex.Match(SubjectString, Pattern).Value;
		SubjectString = SubjectString.Replace(MatchedString.Substring(0, (CharsToBreakAfter - 1)), MatchedString.Substring(0, (CharsToBreakAfter - 1)) + " ");

		// Prevent endless loops
		if (Counter > 20) break;

		// Check if we still have long strings
		IsMatching = Regex.IsMatch(SubjectString, Pattern);
	}

	return SubjectString;
}

Get last MySQL AUTO_INCREMENT ID

// Get last MySQL AUTO_INCREMENT ID
// http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

SELECT LAST_INSERT_ID();
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS