• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一个php Mysql类 可以参考学习熟悉下_php实例

php 搞代码 4年前 (2022-01-26) 34次浏览 已收录 0个评论
<?php <BR>class Mysql <BR>{ <BR>private $conn; <BR>private $host; <BR>private $username; <BR>private $password; <BR>private $dbname; <BR>private $pconnect; <BR>private $charset; <br><br>public function __construct(array $params = null) <BR>{ <BR>if (!empty($params)) { <BR>foreach ($params as $k => $v) { <BR>$this->$k = $v; <BR>} <BR>} <BR>} <br><br>public function connect() <BR>{ <BR>$fun = $this->pconnect ? 'mysql_pconnect' : 'mysql_connect'; <BR>$this->conn = $fun($this->host, $this->username, $this->password); <BR>$this->conn && $this->query('set names ' . $this->charset); <BR>$this->conn && mysql_select_db($this->dbname, $this->conn); <BR>} <br><br>public function getInstance() <BR>{ <BR>return $this->conn; <BR>} <br><br>public function query($sql) <BR>{ <BR>return mysql_query($sql, $this->conn); <BR>} <br><br>public function fetchOne($sql) <BR>{ <BR>$data = $this->fetchRow($sql); <BR>return $data[0]; <BR>} <br><br>public function fetchCol($sql) <BR>{ <BR>$tmp = $this->fetchAll($sql, MYSQL_NUM); <BR>foreach ($tmp as $v) { <BR>$data[] = $v[0]; <BR>} <BR>} <br><br>public function fetchRow($sql) <BR>{ <BR>$result = $this->query($sql); <BR>$data = mysql_fetch_row($result); <BR>mysql_free_result($result); <BR>return $data; <BR>} <br><br>public function fetchAssoc($sql) <BR>{ <BR>$result = $this->query($sql); <BR>$data = mysql_fetch_assoc($result); <BR>mysql_free_result($result); <BR>return $data; <BR>} <br><br>public function fetchAll($sql, $type = MYSQL_ASSOC) <BR>{ <BR>$result = $this->query($sql); <BR>while ($tmp = mysql_fetch_array($result, $type)) { <BR>$data[] = $tmp; <BR>} <BR>return $data; <BR>} <br><br>public function fetchPairs($sql) <BR>{ <BR>$result = $this->query($sql); <BR>while ($tmp = mysql_fetch_row($result)) { <BR>$data[$tmp[0]] = $tmp[1]; <BR>} <BR>return $data; <br><br>} <br><br>public function insert($table, array $bind) <BR>{ <BR>$cols = array(); <BR>$vals = array(); <BR>foreach ($bind as $col => $val) { <BR>$cols[] = $col; <BR>$vals[] = $val; <BR>unset($bind[$col]); <BR>} <BR>$sql = "INSERT INTO " <BR>. $table <BR>. ' (`' . implode('`, `', $cols) . '`) ' <BR>. 'VALUES (\'' . implode('\', \'', $vals) . '\')'; <br><br>$stmt = $this->query($sql, $this->conn); <BR>$result = $this->affectedRows(); <BR>return $result; <BR>} <br><br>public function getLastInsertId() <BR>{ <BR>return mysql_insert_id($this->conn); <BR>} <br><br>public function affectedRows() <BR>{ <BR>return mysql_affected_rows($this->conn); <BR>} <br><br>public function update($table, array $bind, $where = '') <BR>{ <BR>$set = array(); <BR>foreach ($bind as $col => $val) { <BR>$set[] = '`' . $col . "` = '" . $val . "'"; <BR>} <br><br>$sql = "UPDATE `" <BR>. $table <BR>. '` SET ' . implode(', ', $set) <BR>. (($where) ? " WHERE $where" : ''); <br><br>$stmt = $this->query($sql, array_values($bind)); <BR>$result = $this->affectedRows(); <BR>return $result; <BR>} <br><br>public function delete($table, $where = '') <BR>{ <BR>/** <BR>* Build the DELETE statement <BR>*/ <BR>$sql = "DELETE FROM " <BR>. $table <BR>. (($where) ? " WHERE $where" : ''); <br><br>/** <BR>* Execute the statement and <span>!本文来源gaodai#ma#com搞*!代#%^码网5</span><pre>搞gaodaima代码

return the number of affected rows
*/
$stmt = $this->query($sql);
$result = $stmt ? mysql_affected_rows($this->conn) : $stmt;
return $result;
}

public function close()
{
$this->conn && mysql_close($this->conn);
}
}
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一个php Mysql类 可以参考学习熟悉下_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址