From: Joe Presbrey Date: Thu, 2 Nov 2006 18:05:12 +0000 (+0000) Subject: git-svn-id: svn://presbrey.mit.edu/php/lib@116 a142d4bd-2cfb-0310-9673-cb33a7e74f58 X-Git-Url: http://andersk.mit.edu/gitweb/sql-web.git/commitdiff_plain/02ae379fba958da7830563fb33a89f30a49eadc6 git-svn-id: svn://presbrey.mit.edu/php/lib@116 a142d4bd-2cfb-0310-9673-cb33a7e74f58 --- diff --git a/lib/joe/joe.lib.php b/lib/joe/joe.lib.php deleted file mode 100644 index dd7be0a..0000000 --- a/lib/joe/joe.lib.php +++ /dev/null @@ -1,211 +0,0 @@ - 0) { - $arr = array(); - if (is_null($key)) { - while ($r = mysql_fetch_assoc($rs)) { - $arr[] = $r; - } - } elseif (is_numeric($key)) { - while ($r = mysql_fetch_row($rs)) { - $arr[$r[$key]] = $r; - } - } else { - while ($r = mysql_fetch_assoc($rs)) { - $arr[$r[$key]] = $r; - } - } - mysql_free_result($rs); - return $arr; - } else { - mysql_free_result($rs); - return array(); - } -} - -function printErrors($err) { printList('err', $err); } -function printMsgs($err) { printList('msg', $err); } - -function printList($class,$err) { - if (is_array($err) && count($err)) { - echo '
',(count($err)>1?'':''),'
'; - } -} - -function buildSQLSet($fields, $values=null, $safeFields=false) { - $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); - $sql = ''; - $c = 0; - if (!is_null($values)) { - foreach($fields as $field) { - if ($c++) $sql .= ','; - $value = array_shift($values); - if (is_numeric($value)) - $sql .= " `$field`=".mysql_real_escape_string($value); - else - $sql .= " `$field`='".mysql_real_escape_string($value)."'"; - } - } else { - foreach($fields as $field=>$value) { - if ($c++) $sql .= ','; - if (in_array($value,$ex) || (is_array($safeFields) && in_array($field,$safeFields))) { - $sql .= " `$field`=$value"; - } elseif (is_numeric($value)) { - $sql .= " `$field`=".mysql_real_escape_string($value); - } else { - $sql .= " `$field`='".mysql_real_escape_string($value)."'"; - } - } - } - return $sql; -} - -function buildSQLInsert($array, $table=null, $safeFields=false) { - $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); - $sql = '('; - $c = 0; - foreach($array as $field=>$value) { - if ($c++) $sql .= ','; - $sql .= " `$field` "; - } - $sql .= ') VALUES ('; - $c = 0; - foreach($array as $field=>$value) { - if ($c++) $sql .= ','; - if (in_array($value, $ex) || (is_array($safeFields) && in_array($field,$safeFields))) { - $sql .= " $value "; - } else { - $value = mysql_real_escape_string($value); - if (is_numeric($value)) { - $sql .= " $value "; - } else { - $sql .= " '$value' "; - } - } - } - $sql .= ')'; - return (is_null($table)?$sql:('INSERT INTO `'.$table.'` '.$sql)); -} - -function build_str($query_array) { - $query_string = array(); - foreach ($query_array as $k => $v) { - $new = $k; - if (strlen($v)) - $new .= '='.$v; - $query_string[] = $new; - } - return join('&', $query_string); -} - -function newQS($key, $val=null) { - return newQSA(array($key=>$val)); -} - -function newQSA($array=array()) { - parse_str($_SERVER['QUERY_STRING'], $arr); - $s = count($arr); - foreach($array as $key=>$val) { - $arr[$key] = $val; - if (is_null($val)) - unset($arr[$key]); - } - return (count($arr)||$s)?'?'.build_str($arr):''; -} - -function formQSA($array=array()) { - if (!count($array)) $array = $_SERVER['QUERY_STRING']; - parse_str($array, $arr); - $text = ''; - foreach($arr as $key=>$val) { - $text .= sprintf('', $key, $val); - } - return $text; -} - -?> diff --git a/lib/joe/joe.lib.php b/lib/joe/joe.lib.php new file mode 120000 index 0000000..42c1507 --- /dev/null +++ b/lib/joe/joe.lib.php @@ -0,0 +1 @@ +util.lib.php \ No newline at end of file diff --git a/lib/joe/util.lib.php b/lib/joe/util.lib.php new file mode 100644 index 0000000..dd7be0a --- /dev/null +++ b/lib/joe/util.lib.php @@ -0,0 +1,211 @@ + 0) { + $arr = array(); + if (is_null($key)) { + while ($r = mysql_fetch_assoc($rs)) { + $arr[] = $r; + } + } elseif (is_numeric($key)) { + while ($r = mysql_fetch_row($rs)) { + $arr[$r[$key]] = $r; + } + } else { + while ($r = mysql_fetch_assoc($rs)) { + $arr[$r[$key]] = $r; + } + } + mysql_free_result($rs); + return $arr; + } else { + mysql_free_result($rs); + return array(); + } +} + +function printErrors($err) { printList('err', $err); } +function printMsgs($err) { printList('msg', $err); } + +function printList($class,$err) { + if (is_array($err) && count($err)) { + echo '
',(count($err)>1?'':''),'
'; + } +} + +function buildSQLSet($fields, $values=null, $safeFields=false) { + $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); + $sql = ''; + $c = 0; + if (!is_null($values)) { + foreach($fields as $field) { + if ($c++) $sql .= ','; + $value = array_shift($values); + if (is_numeric($value)) + $sql .= " `$field`=".mysql_real_escape_string($value); + else + $sql .= " `$field`='".mysql_real_escape_string($value)."'"; + } + } else { + foreach($fields as $field=>$value) { + if ($c++) $sql .= ','; + if (in_array($value,$ex) || (is_array($safeFields) && in_array($field,$safeFields))) { + $sql .= " `$field`=$value"; + } elseif (is_numeric($value)) { + $sql .= " `$field`=".mysql_real_escape_string($value); + } else { + $sql .= " `$field`='".mysql_real_escape_string($value)."'"; + } + } + } + return $sql; +} + +function buildSQLInsert($array, $table=null, $safeFields=false) { + $ex = array('NOW()','NULL','/FROM_UNIXTIME\(\d+\)/'); + $sql = '('; + $c = 0; + foreach($array as $field=>$value) { + if ($c++) $sql .= ','; + $sql .= " `$field` "; + } + $sql .= ') VALUES ('; + $c = 0; + foreach($array as $field=>$value) { + if ($c++) $sql .= ','; + if (in_array($value, $ex) || (is_array($safeFields) && in_array($field,$safeFields))) { + $sql .= " $value "; + } else { + $value = mysql_real_escape_string($value); + if (is_numeric($value)) { + $sql .= " $value "; + } else { + $sql .= " '$value' "; + } + } + } + $sql .= ')'; + return (is_null($table)?$sql:('INSERT INTO `'.$table.'` '.$sql)); +} + +function build_str($query_array) { + $query_string = array(); + foreach ($query_array as $k => $v) { + $new = $k; + if (strlen($v)) + $new .= '='.$v; + $query_string[] = $new; + } + return join('&', $query_string); +} + +function newQS($key, $val=null) { + return newQSA(array($key=>$val)); +} + +function newQSA($array=array()) { + parse_str($_SERVER['QUERY_STRING'], $arr); + $s = count($arr); + foreach($array as $key=>$val) { + $arr[$key] = $val; + if (is_null($val)) + unset($arr[$key]); + } + return (count($arr)||$s)?'?'.build_str($arr):''; +} + +function formQSA($array=array()) { + if (!count($array)) $array = $_SERVER['QUERY_STRING']; + parse_str($array, $arr); + $text = ''; + foreach($arr as $key=>$val) { + $text .= sprintf('', $key, $val); + } + return $text; +} + +?>