Beispiele datasource.inc

Hier ein paar Beispiele, wie man die datasource-Klasse anwenden kann.
Als Erstes erzeugen wir eine Tabele und fügen Daten ein. Man beachte hierbei auf welche Weise die Daten eingefügt werden können.

Insert Beispiele

example_insert.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?
include('ddatasource.inc');

$ds = new database(array ('type' => 'db''driver' => 'mysql''host' => 'localhost''port' => '3306''user' => 'dbuser''password' => 'dbpass','db' => 'db-subdb'));

$ds->addTable('playlist', array(    'id'    => array(TYP_INTNOT_NULLAUTOPK), 
                                
'interpret' => array(TYP_VARCHAR255NOT_NULL) ,
                                
'song'  => array(TYP_TEXT,  NOT_NULL) ) ); 


$id1 $ds->insert(array('interpret'=>'Bily Talent','song'=>"Faling Leaves"),'playlist');
$id2 $ds->insert(array('interpret'=>'Rammstein','song'=>"Du riechst so gut"),'playlist');
$ids $ds->insert(array(    array('interpret'=>'Emil Bulls','song'=>"Smells Like Rock n Roll"),
                            array(
'interpret'=>'Emil Bulls','song'=>"Leaving You with This")),'playlist');

$id3 $ds->insert(array('5','ASP','Ich will brennen'),'playlist');
$id4 $ds->insert(array('','Guano Apes','Open your eyes'),'playlist');
$ids2 $ds->insert(array(    array('','Kitty In A Casket','Bloody Lovesong'), 
                            array(
'','Kitty In A Casket','Horror Express'), 
                            array(
'','Kitty In A Casket','Bride of the Monster')),'playlist');

$ds->select('*','playlist')->toHtmlTable()?

Ausgeführe Querys:

CREATE TABLE `playlist` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `interpret` VARCHAR (255) NOT NULL , `song` TEXT NOT NULL )
new Statement:INSERT INTO `playlist` (interpret,song) VALUES (?,?) Array ( [0] => Bily Talent [1] => Faling Leaves )
old Statement:INSERT INTO `playlist` (interpret,song) VALUES (?,?) Array ( [0] => Rammstein [1] => Du riechst so gut )
old Statement:INSERT INTO `playlist` (interpret,song) VALUES (?,?) Array ( [0] => Emil Bulls [1] => Smells Like Rock n Roll )
old Statement:INSERT INTO `playlist` (interpret,song) VALUES (?,?) Array ( [0] => Emil Bulls [1] => Leaving You with This )
new Statement:INSERT INTO `playlist` VALUES (?,?,?) Array ( [0] => 5 [1] => ASP [2] => Ich will brennen )
old Statement:INSERT INTO `playlist` VALUES (?,?,?) Array ( [0] => [1] => Guano Apes [2] => Open your eyes )
old Statement:INSERT INTO `playlist` VALUES (?,?,?) Array ( [0] => [1] => Kitty In A Casket [2] => Bloody Lovesong )
old Statement:INSERT INTO `playlist` VALUES (?,?,?) Array ( [0] => [1] => Kitty In A Casket [2] => Horror Express )
old Statement:INSERT INTO `playlist` VALUES (?,?,?) Array ( [0] => [1] => Kitty In A Casket [2] => Bride of the Monster )
new Statement:SELECT * FROM `playlist` Array ()

Daten nach dem Insert:

idinterpretsong
Bily Talent Faling Leaves 
Rammstein Du riechst so gut 
Emil Bulls Smells Like Rock n Roll 
Emil Bulls Leaving You with This 
ASP Ich will brennen 
Guano Apes Open your eyes 
Kitty In A Casket Bloody Lovesong 
Kitty In A Casket Horror Express 
Kitty In A Casket Bride of the Monster 

Update Beispiel

example_update.inc
1
2
3
4
5
6
7
8
9
10
11
12
<?

$ds
->update(array('interpret'=>"Billy Talent",'song'=>"Falling Leaves"),'playlist'$id1);
$ds->update(array(    array('song'=>"Leaving You with This"), 
                    array(
'song'=>'Smells Like Rock n Roll')),'playlist'$ids);

$ds->update('id = id + 100','playlist''');

print_r(    $ds->SELECT('*')->FROM('playlist')->WHERE(array('interpret'=>"Emil Bulls") )->execute()->toArray('id') );

$ds->FROM('playlist')->execute()->toHtmlTable()

Ausgeführe Querys:

new Statement:UPDATE `playlist` SET `interpret` = ?,`song` = ? WHERE id=? Array ( [0] => Billy Talent [1] => Falling Leaves [2] => 1 )
new Statement:UPDATE `playlist` SET `song` = ? WHERE id=? Array ( [0] => Leaving You with This [1] => 3 )
old Statement:UPDATE `playlist` SET `song` = ? WHERE id=? Array ( [0] => Smells Like Rock n Roll [1] => 4 )
new Statement:UPDATE `playlist` SET id = id + 100 Array ()
new Statement:SELECT * FROM `playlist` WHERE interpret=? Array ( [0] => Emil Bulls )
old Statement:SELECT * FROM `playlist` Array ()

print_r:

Array (
  [103] => Array ( [id] => 103 [interpret] => Emil Bulls [song] => Leaving You with This )
  [104] => Array ( [id] => 104 [interpret] => Emil Bulls [song] => Smells Like Rock n Roll)
)

Daten nach dem Update:

idinterpretsong
101 Billy Talent Falling Leaves 
102 Rammstein Du riechst so gut 
103 Emil Bulls Leaving You with This 
104 Emil Bulls Smells Like Rock n Roll 
105 ASP Ich will brennen 
106 Guano Apes Open your eyes 
107 Kitty In A Casket Bloody Lovesong 
108 Kitty In A Casket Horror Express 
109 Kitty In A Casket Bride of the Monster 

Delete Beispiel

example_delete.inc
1
2
3
4
5
6
7
8
9
<?

$ds
->delete('playlist', array('id'=>'<=102'));
$ds->delete('playlist', array('interpret'=>'like%In A%'));

$ds->FROM('playlist')->WHERE('id > 100')->execute()->toHtmlTable()

$ds->delTable('playlist'); 

Daten nach dem Löschen:

idinterpretsong
103 Emil Bulls Leaving You with This 
104 Emil Bulls Smells Like Rock ?n? Roll 
105 ASP Ich will brennen 
106 Guano Apes Open your eyes 

Ausgeführe Querys:

new Statement:DELETE FROM `playlist` WHERE id<=? Array( [0] => 102 )
new Statement:DELETE FROM `playlist` WHERE interpret like ? Array ( [0] => %In A%)
new Statement:SELECT * FROM `playlist` WHERE id > 100 Array ()
DROP TABLE `playlist`