[PHP]
<?php
/*************** *************** *
Database Querying Class
*************** *************** **/
define('TEST',0 );
define('STUDENT _PROFILE',1);
define('CICO',2 );
//define('STUDENT _PROFILE_100',3 );
define('VPOPMAI L',4);
define('ADMIN_A T_STUDENT',5);
define('CMS',6) ;
define('ADMIN_A T_STAFF',7);
define('USER_AT _STAFF',8);
define('USER_AT _STUDENT',9);
define('USER_AT _VPOPMAIL',10);
define('ADMIN_A T_VPOPMAIL',11) ;
define('ADMIN_A T_AMR',13);
define('USER_AT _AMR',14);
define('AMR_TES T_USER',16);
class MrDB
{
private $host;
private $db;
private $user;
private $pass;
private $link;
public $sql; // the last sql
public $result; // this result set hold the latest retrieved result
public $row; // holding the last row fetched
public $numOfRow; // number of rows in the result set (false on fail, or number of affected rows)
public $rsArray; // convert result set to array for processing
public $mydate; //check date
public function getLink()
{
return $this->link;
}
// ------------------------------------------------------------------------------
public function __construct($db Num=0, $newLink=false)
{
// use absolute path so that the script can be run in console
//include_once($_ SERVER['DOCUMENT_ROOT'].'/../htsecret/dbCfg.inc.php') ;
//include('/var/www/htsecret/dbCfg.inc.php') ;
$this->host = 'localhost';
$this->db = 'prosearch';
$this->user = 'root';
$this->pass = 'hkic';
if(!($this->link = mysql_connect($ this->host, $this->user, $this->pass, $newLink)))
{
echo "Connect to server failure!";
echo mysql_errno($th is->link)." : ".mysql_error($ this->link)."\n";
exit("mysql connection error");
}
if(!mysql_selec t_db($this->db, $this->link))
{
echo "Connect to database failure!";
echo mysql_errno($th is->link)." : ".mysql_error($ this->link)."\n";
exit("mysql select DB error");
}
mysql_query('SE T CHARACTER SET big5', $this->link);
// or die('Query1 failed: '.mysql_error() )
// Replaced by PHP5 destructor
//register_shutdo wn_function(arr ay(&$this, 'close'));
}
// ------------------------------------------------------------------------------
public function __destruct()
{
@mysql_free_res ult($this->result);
mysql_close($th is->link);
}
// ------------------------------------------------------------------------------
public function query($query="s how tables")
{
if(!($this->result = mysql_query($qu ery, $this->link)))
{
echo "Query to database failure!";
echo mysql_errno($th is->link)." : ".mysql_error($ this->link)."\n";
exit("mysql query error");
}
$this->sql = $query;
// also keep track the no. of rows after each query
$firstWord = explode(" ", $query);
switch(strtolow er($firstWord[0]))
{
case 'select':
{
// mysql_num_rows returns false on fail
$this->numOfRow = mysql_num_rows( $this->result);
break;
}
case 'insert':
case 'update':
case 'delete':
{
// mysql_affected_ rows return -1 on fail
$affected = mysql_affected_ rows($this->link);
$this->numOfRow = ($affected == -1) ? false : $affected;
break;
}
default:
{
$this->numOfRow = null;
}
}
return $this->result;
}
// ------------------------------------------------------------------------------
// Escape variable of sql to make safe
public function escapeSql($valu e)
{
// Stripslashes
if(get_magic_qu otes_gpc())
$value = stripslashes($v alue);
return mysql_real_esca pe_string($valu e);
}
// ------------------------------------------------------------------------------
// Strip slashes recursively from value which can be an array
public function stripRecur($val ue)
{
// this function would cause error when strip some chinese characters e.g. ³\
/*
if(is_array($va lue))
$value = array_map(array ('MrDB','stripR ecur'), $value);
elseif(!empty($ value) && is_string($valu e))
$value = stripslashes($v alue);
*/
return $value;
}
// ------------------------------------------------------------------------------
// $table - target table name
// $row - an array of fields and values e.g. array('id'=>10, 'name'=>'Peter' )
public function insert($table, $row)
{
// build the fore part
$fore = "insert into `$table` set ";
// build the rare part
foreach($row as $f => $v)
{
$v = $this->escapeSql($v );
$temp[] = "`$f` = '$v'";
}
$rare = implode($temp,' , ');
$sql = "$fore $rare";
$this->query($sql);
return $this->numOfRow;
}
// ------------------------------------------------------------------------------
// Similar to the method "insert" with additional $keys to specify which element of $row is used as unqiue keys
// e.g. $keys = array('id') or $keys = array('id1','id 2')
public function update($table, $row, $keys)
{
// build the fore part
$fore = "update `$table` set ";
// build the remaining part
foreach($row as $f => $v)
{
$v = $this->escapeSql($v );
if(in_array($f, $keys))
$temp2[] = "`$f` = '$v'";
else
$temp1[] = "`$f` = '$v'";
}
$middle = implode($temp1, ', ');
$rare = implode($temp2, ' and ');
$sql = "$fore $middle where $rare";
$this->query($sql);
return $this->numOfRow;
}
// ------------------------------------------------------------------------------
public function resultToArray($ type='assoc')
{
// go to row 0
mysql_data_seek ($this->result, 0);
// destory the array first
unset($this->rsArray);
switch(strtolow er($type))
{
case 'assoc':
{
while($tempRow= mysql_fetch_ass oc($this->result))
$this->rsArray[] = $this->stripRecur($te mpRow);
break;
}
case 'row':
{
while($tempRow= mysql_fetch_row ($this->result))
$this->rsArray[] = $this->stripRecur($te mpRow);
break;
}
case 'array':
default:
{
while($tempRow= mysql_fetch_arr ay($this->result))
$this->rsArray[] = $this->stripRecur($te mpRow);
break;
}
}
// go back to row 0 before return
mysql_data_seek ($this->result, 0);
return $this->rsArray;
}
// ------------------------------------------------------------------------------
// These 3 functions are just wrapper functions for more OO implementation
public function fetchRow($key=f alse)
{
$tempRow = mysql_fetch_row ($this->result);
$this->row = $this->stripRecur($te mpRow);
return (($key===false) ?($this->row):($this->row[$key]));
}
public function fetchAssoc($key =false)
{
$tempRow = mysql_fetch_ass oc($this->result);
$this->row = $this->stripRecur($te mpRow);
return (($key===false) ?($this->row):($this->row[$key]));
}
public function fetchArray($key =false)
{
$tempRow = mysql_fetch_arr ay($this->result);
$this->row = $this->stripRecur($te mpRow);
return (($key===false) ?($this->row):($this->row[$key]));
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-- check valid date value
public function checkData($myda te)
{
list($yy,$mm,$d d)=explode("-",$mydate);
if (is_numeric($yy ) && is_numeric($mm) && is_numeric($dd) )
{
return checkdate($mm,$ dd,$yy);
}
return false;
}
}
?>
[/PHP]
This is the Whole Class that i hv used
And i do the following code in below
[PHP]
$new_app = array();
foreach($_POST as $var=>$value)
{
$new_app[$var]=$value;
}
$new_app_id=sav e_new_app($new_ app);
//$new_app_id=0;
$new_app['app_id']=$new_app_id;
$_SESSION['app_id']=$new_app_id;
[/PHP]
with the function in here
[PHP]
function save_new_app($n ew_app)
{
$db = new MrDB();
$new_app_id=get _new_app_id();
$sql="INSERT INTO `" . $GLOBALS['ygtables']. "` (`app_id`) VALUES ('".$new_app_id ."')" ;
//echo $sql;
$db->query($sql);
//echo "GOT $new_app_id";
foreach($new_ap p as $var=>$value)
{
$sql="UPDATE `" . $GLOBALS['ygtables']. "` SET `".$var."`='".$ value."' where `app_id`='".$ne w_app_id."'";
$db->query($sql);
}
return $new_app_id;
}
[/PHP]
I do the $db->query before this one and it is completely OK.
the problem is when i run this code, the firefox say "waiting for localhost"
and then a long time of waiting and the dead of apache...
What do you mean by it hangs? How far into the function does it get?
And, so you know, you may want to reorganize your class. I'd direct you to a long, informative post we had at DevNetwork on writing database classes, but I can't seem to find the link.
EDIT: Found it.
All of us pitched in to help him understand exactly how the database class should work.