Magellan Linux

Contents of /alx-src/trunk/alx-web/include/basesql.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 355 - (show annotations) (download)
Mon Oct 10 19:42:06 2005 UTC (18 years, 8 months ago) by niro
File size: 2010 byte(s)
cvs import: alx-web for alxconfig-ng
 - code clean up and
 - reorganized the whole structure

1 <?
2 // Verbinden
3 include('dbconn.php');
4
5 // Codiert die Anführungszeichen in einem String, sodass man ihn
6 // in einem SQL-Aufruf einbinden kann
7 function redir($str)
8 {
9 $slash = "\\";
10 return str_replace("'", $slash."'", str_replace($slash, $slash.$slash, $str));
11 }
12
13 function updatestr($arr)
14 {
15 $res = '';
16 foreach($arr as $k=>$v)
17 { if($res!='') $res.=','; $res.=$k.'=\''.redir($v).'\''; }
18 return $res;
19 }
20
21 // Fügt der Tabelle $db einen neuen Datensatz mit den Werten des asso. Arrays $vals hinzu
22 function sqladd($db, $vals)
23 {
24 $q = "INSERT INTO $db (";
25 $i = 0; foreach($vals as $k=>$v) { if($i>0) { $q.=','; } $q .= redir($k); $i++; }
26 $q .= ") VALUES (";
27 $i = 0; foreach($vals as $k=>$v) { if($i>0) { $q.=','; } $q .= "'".redir($v)."'"; $i++; }
28 $q .= ');';
29
30 $result = mysql_query($q) or die("Anfrage fehlgeschlagen: " . mysql_error());
31 }
32
33 // Führt eine Callbackfunktion für alle Elemente eines Arrays aus
34 function arrforeach(&$arr, $func, $params)
35 {
36 foreach($arr as $k=>$v)
37 { $func($arr[$k], $params); }
38 }
39
40 // Holt ein array mit den Results einer MySQL-Abfrage
41 function sqlarr($query, $arrkey='')
42 {
43 $arr = array();
44
45 $result = mysql_query($query) or die("Anfrage fehlgeschlagen: " . mysql_error());
46 while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
47 {
48 if($arrkey=='') $arr[] = $line;
49 else $arr[$line[$arrkey]] = $line;
50 }
51
52 return $arr;
53 }
54
55 // Kopplung zw. arrforeach(sqlarr(...))
56 function sqlforeach($query, $func, $params)
57 { arrforeach(sqlarr($query), $func, $params); }
58
59 // ...
60 function sqlfirst($query, $prop='')
61 {
62 $result = mysql_query($query) or die("Anfrage fehlgeschlagen: " . mysql_error());
63 if($line = mysql_fetch_array($result, MYSQL_ASSOC)) $res = $line;
64 else $res = array();
65
66 if($prop!='') $res = $res[$prop];
67 return $res;
68 }
69
70 // ...
71 function sqlcount($what)
72 {
73 $res = sqlfirst('SELECT count(*) c FROM '.$what);
74 return $res['c'];
75 }
76 ?>

Properties

Name Value
svn:executable *