INSERT query builder dmv value array
Auteur: marten - 19 september 2007 - 08:47 - Gekeurd door: Stijn - Hits: 4955 - Aantal punten: 1.50 (2 stemmen)
Een handige functie die ik nu een tijdje gebruik. De functie wil graag van je weten welke tabel je wilt vullen en een array met de waardes.
Een array dient als volgt opgebouwd te worden:
$array['veldnaam_uit_database'] = 'value dat in de database moet';
De velden die niet goed zijn worden netjes overgeslagen. Dus alleen de velden die in de database tabel staan worden verwerkt.
$pdo staat een PDO connectie.
Code:
/**
* Sets the database link identifier
*
* @subpackage Author
* @author Marten van Urk .: ComSi www.comsi.nl :.
* @param String $host
* @param String $user
* @param String $pass
* @param String $database
*/
public function setDb($host, $user, $pass, $database) {
try {
$pdo = new PDO('mysql:host=' .$host. ';dbname=' .$database. '', '' .$user. '', '' .$pass. '' , array(PDO::ATTR_PERSISTENT => true));
$this->db = $pdo;
} catch (Exception $e) {
return false;
}
}
/**
* Insert query builder for news system
*
* @author Marten van Urk .: ComSi www.comsi.nl :.
* @param Array $values
* @param String $table
* @return Boolean
*/
public function insertFunction($values, $table) {
$sQuery_fields = "SHOW COLUMNS FROM " . $table;
try {
$rResult_fields = $this->db->query($sQuery_fields);
$sFields = '';
$aFields = array();
$sValues = '';
$looped = 1;
$count = count($values);
foreach ($rResult_fields as $r) {
$aFields[$r[0]] = '';
}
foreach ($values as $key => $value) {
if (array_key_exists($key, $aFields)) {
if ($looped < $count) {
$sFields .= $key . ', ';
$sValues .= "'" .$value. "',";
} elseif ($looped == $count) {
$sFields .= $key;
$sValues .= "'" .$value. "'";
}
}
$looped++;
}
$sQuery = "INSERT INTO " .$table. " (" .$sFields. ") VALUES (" .$sValues. ")";
$rResult = $this->db->query($sQuery);
return true;
} catch (Exception $e) {
return false;
}
}
/**
* Sets the database link identifier
*
* @subpackage Author
* @author Marten van Urk .: ComSi www.comsi.nl :.
* @param String $host
* @param String $user
* @param String $pass
* @param String $database
*/
public function setDb( $host , $user , $pass , $database ) {
try {
$pdo = new PDO
( 'mysql:host=' . $host . ';dbname=' . $database . '' , '' . $user . '' , '' . $pass . '' , array ( PDO
:: ATTR_PERSISTENT => true ) ) ; $this -> db = $pdo ;
} catch ( Exception $e ) {
return false ;
}
}
/**
* Insert query builder for news system
*
* @author Marten van Urk .: ComSi www.comsi.nl :.
* @param Array $values
* @param String $table
* @return Boolean
*/
public function insertFunction( $values , $table ) {
$sQuery_fields = "SHOW COLUMNS FROM " . $table ;
try {
$rResult_fields = $this -> db -> query ( $sQuery_fields ) ;
$sFields = '' ;
$sValues = '' ;
$looped = 1 ;
foreach ( $rResult_fields as $r ) {
$aFields [ $r [ 0 ] ] = '' ;
}
foreach ( $values as $key => $value ) {
if ( $looped < $count ) {
$sFields .= $key . ', ' ;
$sValues .= "'" . $value . "'," ;
} elseif ( $looped == $count ) {
$sFields .= $key ;
$sValues .= "'" . $value . "'" ;
}
}
$looped ++;
}
$sQuery = "INSERT INTO " . $table . " (" . $sFields . ") VALUES (" . $sValues . ")" ;
$rResult = $this -> db -> query ( $sQuery ) ;
return true ;
} catch ( Exception $e ) {
return false ;
}
}
Download code (.txt)
Stemmen
Niet ingelogd.