|
|
|
MySQL Erweiterung: Insert Funktion
Zurück zur Übersicht MySQL Erweiterung: Insert-Funktion
Seitdem ich bemerkt habe, dass bei vielen Spalten einem MySQL Syntax die Übersicht manchmal vergeht, habe ich mir eine Insert-Funktion gebastelt. Sie kann überall angepasst werden, wie zum Beispiel in einer Klasse als Methode.
Zunächst einmal, hier ist die vollständige Funktion:
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
|
<?php
/*
* [mysql]insert function
* Copyright (C) 2010 Maya <www.makesite.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/*
* @function insert
* @param table, insert[, auto_exec = false]]
*/
function insert($table, array $input, $auto_exec = false)
{
// make sure $table is a string and input is an array
if(is_string($table)&&is_array($input))
{
/*
* strip array
*/
$array_keys = array_keys($input); // get keys into $array_keys
$array_values = array_values($input); // get values into $array_values
/*
* starting syntax
*/
$column = sprintf("%s",$array_keys[0]); // saving column 1
$values = sprintf("'%s'",$array_values[0]); // saving value 1
/*
* drop first key of array $input
*/
unset($input[$array_keys[0]]);
/*
* set the same vars like array_keys and array_values
*/
$cols = array_keys($input); // set array again ($array_keys)
$vals = array_values($input); // set array again ($array_values)
/*
* starting loop
*/
for($int = (integer)0; $int < count($input); ++$int)
{
$column .= sprintf(", %s", $cols[$int]); // add column
$values .= sprintf(", '%s'", $vals[$int]); // add key
}
/*
* complete syntax
*/
$syntax = sprintf("INSERT INTO %s (%s) VALUES (%s)", $table, $column, $values);
/*
* check if you want auto execute
*
* WARNING: IF YOU DONT USE mysql_query() PLEASE RENAME IT
*/
if($auto_exec)
return @mysql_query($syntax); // doing query
else
return $syntax; // return syntax only
}
else
{
/*
* you will get false if table isnt a string or if input isnt an array
*/
return false;
}
}
?>
|
|
Verwendung
Funktion insert() enthält drei Parameter, davon ist eine optional.
string table: Name der Tabelle, wo die Daten eingespeichert werden sollen.
array input: Array mit den Daten:
array input (
spalte => inhalt
)
optional: bool auto_exec: automatische MySQL Query an Server
Besonders praktisch ist die Funktion für Registrierungen. Wenn ein Formular abgesendet wurde, ist es möglich (wenn die Namen der Felder den Spalten entsprechen) die Variabel $_POST zur Speicherung der Daten zu verwenden:
| PHP |
1:
2:
|
<?
insert('tabellen_name', $_POST, true);
|
|
Bei 10 Datensätzen mit jeweils 5 Spalten
| PHP |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
|
<?
// starting benchmark
$benchmark = new benchmark();
// 100 times using insert()
for($int = (integer)0; $int < 10; ++$int)
{
/*
* 5 columns and 5 values
*/
(array)$array['col1'] = (string)'val';
(array)$array['col2'] = (string)'val';
(array)$array['col3'] = (string)'val';
(array)$array['col4'] = (string)'val';
(array)$array['col5'] = (string)'val';
/*
* results
*/
insert('test', $array);
print 'Laufzeit: ' .$benchmark -> runtime(). '<br/>';
}
print 'Endergebnis: '.$benchmark -> runtime();
?>
|
|
kommt folgendes Ergebnis raus:
| Zitat: |
Laufzeit: 9.3E-5
Laufzeit: 0.000133
Laufzeit: 0.000159
Laufzeit: 0.000182
Laufzeit: 0.000206
Laufzeit: 0.00023
Laufzeit: 0.000254
Laufzeit: 0.000277
Laufzeit: 0.000302
Laufzeit: 0.000326
Endergebnis: 0.000332 |
Copyright (C) 2010 Maya under GPL
Kommentare
Zurück zur Übersicht
Autor Gast
|
|
|
|