Dear all,
i m new in here and hello everyone. I have some problem in here.
I am using a database class. it work fine for sometime but sometime it say invalid Mysql link resource and i dont know whats wrong of my code. Can anyone help ?
Any help i am really appreciating.
$smarty = new MrTemplate();
$student = new MrStudent;
unset($db);
$db = new MrDB(ADMIN_AT_STUDENT);
//start of the code
function showtitle()
{
$sql = "SELECT * FROM `candidate` WHERE `can_id`=\'5197\'";
$db->query($sql);
$arr = $db->fetchAssoc();
$key = array_keys($arr);
print_r($key);
}
my database class
public function __construct($dbNum=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 = $dbCfg[$dbNum]['host'];
$this->db = $dbCfg[$dbNum]['db'];
$this->user = $dbCfg[$dbNum]['user'];
$this->pass = $dbCfg[$dbNum]['pass'];
if(!($this->link = mysql_connect($this->host, $this->user, $this->pass, $newLink)))
{
echo "Connect to server failure!";
echo mysql_errno($this->link)." : ".mysql_error($this->link)."\n";
exit("mysql connection error");
}
if(!mysql_select_db($this->db, $this->link))
{
echo "Connect to database failure!";
echo mysql_errno($this->link)." : ".mysql_error($this->link)."\n";
exit("mysql select DB error");
}
mysql_query('SET CHARACTER SET big5', $this->link);
// or die('Query1 failed: '.mysql_error())
// Replaced by PHP5 destructor
//register_shutdown_function(array(&$this, 'close'));
}
// ------------------------------------------------------------------------------
public function __destruct()
{
@mysql_free_result($this->result);
mysql_close($this->link);
}
// ------------------------------------------------------------------------------
public function query($query="show tables")
{
if(!($this->result = mysql_query($query, $this->link)))
{
echo "Query to database failure!";
echo mysql_errno($this->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(strtolower($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($value)
{
// Stripslashes
if(get_magic_quotes_gpc())
$value = stripslashes($value);
return mysql_real_escape_string($value);
}
// ------------------------------------------------------------------------------
// Strip slashes recursively from value which can be an array
public function stripRecur($value)
{
// this function would cause error when strip some chinese characters e.g. 許
/*
if(is_array($value))
$value = array_map(array('MrDB','stripRecur'), $value);
elseif(!empty($value) && is_string($value))
$value = stripslashes($value);
*/
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','id2')
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(strtolower($type))
{
case 'assoc':
{
while($tempRow=mysql_fetch_assoc($this->result))
$this->rsArray[] = $this->stripRecur($tempRow);
break;
}
case 'row':
{
while($tempRow=mysql_fetch_row($this->result))
$this->rsArray[] = $this->stripRecur($tempRow);
break;
}
case 'array':
default:
{
while($tempRow=mysql_fetch_array($this->result))
$this->rsArray[] = $this->stripRecur($tempRow);
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=false)
{
$tempRow = mysql_fetch_row($this->result);
$this->row = $this->stripRecur($tempRow);
return (($key===false)?($this->row):($this->row[$key]));
}
public function fetchAssoc($key=false)
{
$tempRow = mysql_fetch_assoc($this->result);
$this->row = $this->stripRecur($tempRow);
return (($key===false)?($this->row):($this->row[$key]));
}
Thanks all