(备忘,非原创)一个简单的php中使用的mysql类

<?php class db_mysql { private $connid = FALSE; function __construct($dbhost, $dbuser, $dbpwd, $dbname = null) { $this->sql_connect($dbhost, $dbuser, $dbpwd, $dbname); } function sql_connect($dbhost, $dbuser, $dbpwd, $dbname = null) { if ($this->connid) { mysql_close($this->connid); } if (!$this->connid = mysql_connect($dbhost, $dbuser, $dbpwd)) { $this->handleError('Can not connect to MySQL server. host: ' . $dbhost . ", user:" . $dbuser.", pwd:".$dbpwd); } mysql_query("set character_set_connection=utf8, character_set_results=utf8, character_set_client=binary", $this->connid); if ($dbname) { $this->sql_select_db($dbname); } } function sql_select_db($dbname) { if (!mysql_select_db($dbname, $this->connid)) { $this->handleError('Cannot use database ' . $dbname); } } function sql_query($sql) { if (!($res = mysql_query($sql, $this->connid))) { $this->handleError('MySQL Query Error', $sql); } return $res; } function sql_insert($sql) { $this->sql_query($sql); return mysql_insert_id(); } function sql_fetch_rows($sql, $result_type = MYSQL_ASSOC) { $res = $this->sql_query($sql); $ret = array(); while ($r = mysql_fetch_array($res, $result_type)) { $ret[] = $r; } mysql_free_result($res); return $ret; } function sql_fetch_one($sql, $result_type = MYSQL_ASSOC) { $res = $this->sql_query($sql); $ret = mysql_fetch_array($res, $result_type); mysql_free_result($res); return $ret; } function sql_fetch_one_cell($sql) { $ret = $this->sql_fetch_one($sql, MYSQL_NUM); return $ret[0]; } function sql_fetch_column($sql, $keyField, $result_type = MYSQL_ASSOC) { $res = $this->sql_query($sql); $ret = array(); while ($r = mysql_fetch_array($res, $result_type)) { if (isset($r[$keyField])) { $ret[] = $r[$keyField]; } } mysql_free_result($res); return $ret; } function sql_fetch_singlemap($sql, $keyField, $result_type = MYSQL_ASSOC) { $res = $this->sql_query($sql); $ret = array(); while ($r = mysql_fetch_array($res, $result_type)) { if (isset($r[$keyField])) { $ret[$r[$keyField]] = $r; } } mysql_free_result($res); return $ret; } function sql_fetch_multimap($sql, $keyField, $result_type = MYSQL_ASSOC) { $res = $this->sql_query($sql); $ret = array(); while ($r = mysql_fetch_array($res, $result_type)) { if (isset($r[$keyField])) { $ret[$r[$keyField]][] = $r; } } mysql_free_result($res); return $ret; } function handleError($message = '', $sql = '') { $err = empty($sql) ? '' : 'MySQL Query:' . $sql; $err .= 'MySQL Error:' . mysql_error(); $err .= ', MySQL Errno:' . mysql_errno(); $err .= ', Message:' . $message; error_log($err); } function __destruct() { if ($this->connid) { mysql_close($this->connid); } } } 

再加上一个函数:

   function sql_check($sql){

$r = sql_query($sql);

        if(empty($r)){

    return false;

}

        if(mysql_num_rows($r) > 0){

return true;

}else{

       return false;

        }

  }

 

如何使用:

 

$dbhost = "localhost";

$dbuser = "test";

$dbpwd = "test";

$dbname = "test";

$db = new db_mysql($dbhost, $dbuser, $dbpwd, $dbname);

 

$sql = "select count(*) from sys_shop where uid = $uid";

$goodNum = $db->sql_fetch_one_cell($sql);

 

unset($db); //使用完后再unset一下,是否有必要?

 

 

你可能感兴趣的:((备忘,非原创)一个简单的php中使用的mysql类)