ホームページ  >  記事  >  バックエンド開発  >  php Mysql クラス。_PHP チュートリアルを参照してください。

php Mysql クラス。_PHP チュートリアルを参照してください。

WBOY
WBOYオリジナル
2016-07-21 15:46:24924ブラウズ

复制代码代码如下:

class Mysql
{
private $conn;
プライベート $host;
プライベート $ユーザー名;
プライベート$パスワード;
プライベート $dbname;
プライベート $pconnect;
プライベート $charset;

public function __construct(array $params = null)
{
if (!empty($params)) {
foreach ($params as $k =>$v) {
$this->$k = $ v;
}
}
}

public function connect()
{
$fun = $this->pconnect ? 'mysql_pconnect' : 'mysql_connect';
$this->conn = $fun($this->ホスト, $this->ユーザー名, $this->パスワード);
$this->conn && $this->query('set names ' . $this->charset);
$this->conn && mysql_select_db($this->dbname, $this->conn);
}

パブリック関数 getInstance()
{
return $this->conn;
}

パブリック関数クエリ($sql)
{
return mysql_query($sql, $this->conn);
}

パブリック関数 fetchOne($sql)
{
$data = $this->fetchRow($sql);
$data[0] を返します;
}

パブリック関数 fetchCol($sql)
{
$tmp = $this->fetchAll($sql, MYSQL_NUM);
foreach ($tmp as $v) {
$data[] = $v[0];
}
}

パブリック関数 fetchRow($sql)
{
$result = $this->query($sql);
$data = mysql_fetch_row($result);
mysql_free_result($result);
$data を返す;
}

パブリック関数 fetchAssoc($sql)
{
$result = $this->query($sql);
$data = mysql_fetch_assoc($result);
mysql_free_result($result);
$data を返す;
}

パブリック関数 fetchAll($sql, $type = MYSQL_ASSOC)
{
$result = $this->query($sql);
while ($tmp = mysql_fetch_array($result, $type)) {
$data[] = $tmp;
}
$data を返します。
}

パブリック関数 fetchPairs($sql)
{
$result = $this->query($sql);
while ($tmp = mysql_fetch_row($result)) {
$data[$tmp[0]] = $tmp[1];
}
$data を返します。

}

public function insert($table, array $bind)
{
$cols = array();
$vals = array();
foreach ($bind as $col => $val) {
$cols[] = $col;
$vals[] = $val;
unset($bind[$col]);
}
$sql = "INSERT INTO"
。 $テーブル
。 ' (`' . implode('`, `', $cols) . '`) '
。 'VALUES ('' . implode('', '', $vals) . '')';

$stmt = $this->query($sql, $this->conn);
$result = $this->affectedRows();
$result を返す;
}

public function getLastInsertId()
{
return mysql_insert_id($this->conn);
}

パブリック関数affectedRows()
{
return mysql_affected_rows($this->conn);
}

public function update($table, array $bind, $where = '')
{
$set = array();
foreach ($bind as $col => $val) {
$set[] = '`' . $col . "` = '" 。 $val 。 "";
}

$sql = "更新 `"
。 $テーブル
。 「セット」。 implode(', ', $set)
。 (($where) ? " WHERE $where" : '');

$stmt = $this->query($sql, array_values($bind));
$result = $this->affectedRows();
$result を返す;
}

public function delete($table, $where = '')
{
/**
* DELETE ステートメントを作成する
*/
$sql = "DELETE FROM "
。 $テーブル
。 (($where) ? " WHERE $where" : '');

/**
* ステートメントを実行し、影響を受けた行の数を返します
*/
$stmt = $this->query($sql);
$結果 = $stmt ? mysql_affected_rows($this->conn) : $stmt;
$result を返す;
}

パブリック関数 close()
{
$this->conn && mysql_close($this->conn);
}
}
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/320169.html技術記事例: ?php class Mysql { private $conn;プライベート $host;プライベート $ユーザー名;プライベート $パスワード;プライベート $dbname;プライベート $pconnect;プライベート $charset;公共の機能...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。