SQL update
This commit is contained in:
parent
dc9aef2fcc
commit
0d1c93bdb5
154
sql.php
154
sql.php
@ -1,4 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/*
|
||||||
|
* $query = sprintf("SELECT firstname, lastname, address, age FROM friends
|
||||||
|
* WHERE firstname='%s' AND lastname='%s'",
|
||||||
|
* mysql_real_escape_string($firstname),
|
||||||
|
* mysql_real_escape_string($lastname));
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
class SQL {
|
class SQL {
|
||||||
private $h;
|
private $h;
|
||||||
private $res = false;
|
private $res = false;
|
||||||
@ -16,12 +24,38 @@ class SQL {
|
|||||||
if (SQL_LOG)
|
if (SQL_LOG)
|
||||||
$this->f = fopen ( 'sql.log', 'w' );
|
$this->f = fopen ( 'sql.log', 'w' );
|
||||||
|
|
||||||
$this->h = new mysqli ( $_m ['host'], $_m ['user'], $_m ['pass'], $_m ['data'] );
|
$this->h = mysql_connect ( $_m ['host'], $_m ['user'], $_m ['pass'] );
|
||||||
if ($this->h->connect_errno) {
|
if ($this->h) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
mysql_select_db ( $_m ['data'] );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
private function prepare($q, $t, $p) {
|
||||||
|
$t = str_split ( $t );
|
||||||
|
$v = array ();
|
||||||
|
$v [] = str_replace ( "?", "%s", $q );
|
||||||
|
foreach ( $t as $i => $c ) {
|
||||||
|
switch ($c) {
|
||||||
|
case 's' :
|
||||||
|
$v [] = "'" . mysql_real_escape_string ( $c ) . "'";
|
||||||
|
break;
|
||||||
|
case 'i' :
|
||||||
|
$v [] = ( int ) $c;
|
||||||
|
break;
|
||||||
|
case 'd' :
|
||||||
|
$v [] = ( double ) $c;
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$res = call_user_func_array ( 'mysql_query', $v );
|
||||||
|
if (! $res) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
}
|
||||||
public function get($que, $t = '', $p = array ()) {
|
public function get($que, $t = '', $p = array ()) {
|
||||||
// echo $que;
|
// echo $que;
|
||||||
$this->cnt_get ++;
|
$this->cnt_get ++;
|
||||||
@ -33,54 +67,17 @@ class SQL {
|
|||||||
' ',
|
' ',
|
||||||
''
|
''
|
||||||
), $que ) . "\n" . print_r ( $p, true ) . "\n\n" );
|
), $que ) . "\n" . print_r ( $p, true ) . "\n\n" );
|
||||||
$statement = $this->h->prepare ( $que );
|
$res = $this->prepare ( $que, $t, $p );
|
||||||
switch (count ( $p )) {
|
|
||||||
case 0 :
|
|
||||||
break;
|
|
||||||
case 1 :
|
|
||||||
$statement->bind_param ( $t, $p [0] );
|
|
||||||
break;
|
|
||||||
case 2 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1] );
|
|
||||||
break;
|
|
||||||
case 3 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2] );
|
|
||||||
break;
|
|
||||||
case 4 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3] );
|
|
||||||
break;
|
|
||||||
case 5 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4] );
|
|
||||||
break;
|
|
||||||
case 6 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5] );
|
|
||||||
break;
|
|
||||||
case 7 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6] );
|
|
||||||
break;
|
|
||||||
case 8 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7] );
|
|
||||||
break;
|
|
||||||
case 9 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8] );
|
|
||||||
break;
|
|
||||||
case 10 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9] );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$statement->execute ();
|
|
||||||
|
|
||||||
$ret = array ();
|
$ret = array ();
|
||||||
|
|
||||||
// print_r($statement);
|
// print_r($statement);
|
||||||
if (isset ( $statement->error ) && $statement->error != '') {
|
if (! $res) {
|
||||||
if (SQL_LOG)
|
if (SQL_LOG) {
|
||||||
fputs ( $this->f, $statement->error );
|
fputs ( $this->f, mysql_error () );
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$result = $statement->get_result ();
|
while ( $row = mysql_fetch_assoc ( $res ) ) {
|
||||||
// print_r($result);
|
|
||||||
while ( $row = $result->fetch_assoc () ) {
|
|
||||||
$ret [] = $row;
|
$ret [] = $row;
|
||||||
}
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
@ -119,7 +116,6 @@ class SQL {
|
|||||||
public function set($que, $t = '', $p = array (), $id = false) {
|
public function set($que, $t = '', $p = array (), $id = false) {
|
||||||
// echo $que;
|
// echo $que;
|
||||||
$this->cnt_set ++;
|
$this->cnt_set ++;
|
||||||
$statement = $this->h->prepare ( $que );
|
|
||||||
if (SQL_LOG)
|
if (SQL_LOG)
|
||||||
fputs ( $this->f, str_replace ( array (
|
fputs ( $this->f, str_replace ( array (
|
||||||
"\n",
|
"\n",
|
||||||
@ -128,72 +124,22 @@ class SQL {
|
|||||||
' ',
|
' ',
|
||||||
''
|
''
|
||||||
), $que ) . "\n" . print_r ( $p, true ) . "\n\n" );
|
), $que ) . "\n" . print_r ( $p, true ) . "\n\n" );
|
||||||
switch (count ( $p )) {
|
$res = $this->prepare ( $que, $t, $p );
|
||||||
case 0 :
|
|
||||||
break;
|
if (! $res) {
|
||||||
case 1 :
|
|
||||||
$statement->bind_param ( $t, $p [0] );
|
|
||||||
break;
|
|
||||||
case 2 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1] );
|
|
||||||
break;
|
|
||||||
case 3 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2] );
|
|
||||||
break;
|
|
||||||
case 4 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3] );
|
|
||||||
break;
|
|
||||||
case 5 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4] );
|
|
||||||
break;
|
|
||||||
case 6 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5] );
|
|
||||||
break;
|
|
||||||
case 7 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6] );
|
|
||||||
break;
|
|
||||||
case 8 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7] );
|
|
||||||
break;
|
|
||||||
case 9 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8] );
|
|
||||||
break;
|
|
||||||
case 10 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9] );
|
|
||||||
break;
|
|
||||||
case 11 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9], $p [10] );
|
|
||||||
break;
|
|
||||||
case 12 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9], $p [10], $p [11] );
|
|
||||||
break;
|
|
||||||
case 13 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9], $p [10], $p [11], $p [12] );
|
|
||||||
break;
|
|
||||||
case 14 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9], $p [10], $p [11], $p [12], $p [13] );
|
|
||||||
break;
|
|
||||||
case 15 :
|
|
||||||
$statement->bind_param ( $t, $p [0], $p [1], $p [2], $p [3], $p [4], $p [5], $p [6], $p [7], $p [8], $p [9], $p [10], $p [11], $p [12], $p [13], $p [14] );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$statement->execute ();
|
|
||||||
if (isset ( $statement->error ) && $statement->error != '') {
|
|
||||||
if (SQL_LOG)
|
if (SQL_LOG)
|
||||||
fputs ( $this->f, $statement->error );
|
fputs ( $this->f, mysql_error () );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($id) {
|
if ($id) {
|
||||||
return $statement->insert_id;
|
return mysql_insert_id ( $res );
|
||||||
} else {
|
} else {
|
||||||
return $statement->affected_rows;
|
return ( bool ) mysql_affected_rows ( $res );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function __destruct() {
|
function __destruct() {
|
||||||
if (SQL_LOG)
|
mysql_close ( $this->h );
|
||||||
$this->h->close ();
|
|
||||||
// echo 'DESTROY';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user