≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

php mysql数据库操作类的简单示例

人气:639 时间:2018-09-20

这篇文章主要为大家详细介绍了php mysql数据库操作类的简单示例,具有一定的参考价值,可以用来参考一下。

一个php mysql数据库操作类,功能不是很完善,但非常有用,感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
经测试代码如下:

<?php
/**
 * mysql数据库操作类
 *
 * @param 
 * @arrange (512.笔记) www.q1010.com
 **/
class Database{
private $host;
private $user;
private $pwd;
private $rows;
private $error;
private $result;
private $dbName;
private $connection;
private $isReady;

public function __construct(){
$this->result = null;
$this->isReady = false;
$this->error = array();
}
public function __destruct(){ @mysql_close($this->connection); }

/* setters */
public function setHost($host){ $this->host = $host; }
public function setUser($user){ $this->user = $user; }
public function setPassword($pwd){ $this->pwd = $pwd; }
public function setDbName($dbName){ $this->dbName = $dbName; }

/* other interfaces */
public function init($host=null,$user=null,$pwd=null,$dbName=null){
if(!isset($host,$user,$pwd,$dbName))
die("Please provide require settings.");
$this->setHost($host);
$this->setUser($user);
$this->setPassword($pwd);
$this->setDbName($dbName);
$this->isReady = true;
}

public function select($dbName){
$this->setDbName($dbName);
mysql_select_db($this->dbName,$this->connection) or die("The said database does not exist.");
}

public function query($sql){
$this->result = mysql_query($sql,$this->connection) or die("Invalid query string!");
}

public function connect(){
if(!$this->isReady) die("not ready to connect");
$this->connection = mysql_connect($this->host,$this->user,$this->pwd) or die("Could not connect to database. please check your credentials.");
$this->select($this->dbName);
$this->query("SET NAMES 'utf8'",$this->connection); //persian support
}

public function isConnected(){
if($this->connection)
return true;
return false;
}

public function disconnect(){
mysql_close($this->connection);
$this->connection = null;
}

public function countRows($selectMode = true){
if($selectMode)
return mysql_num_rows($this->result);
return mysql_affected_rows($this->connection);
}

public function loadRows(){
if(!$this->result) die("Nothing found!");
$this->rows = array();
while($r = mysql_fetch_array($this->result,MYSQL_BOTH))
$this->rows[] = $r;
mysql_free_result($this->result);
return $this->rows;
}

public function siftDown($dataStack){
if(!is_array($dataStack)){
$dataStack = ereg_replace("[\'\")(;|`,<>]","",$dataStack);
$dataStack = mysql_real_escape_string(trim($dataStack),$this->connection);
$dataStack = stripslashes($dataStack);
return $dataStack;
}
$safeData = array();
foreach($dataStack as $p=>$data){
$data = ereg_replace("[\'\")(;|`,<>]","",$data);
$data = mysql_real_escape_string(trim($data),$this->connection);
$data = stripslashes($data);
$safeData[$p] = $data;
}
return $safeData;
}

public function secure($data){
return sha1(md5(sha1(md5(sha1($data)))));
}
}//Database class
?>

<?php //usage
require_once 'path/to/Database.class.php';
$db = new Database(); //Creating new object
$db->init("localhost","test_root","test_pwd!","test_db"); //initializing by credentials.
$db->connect(); //unicode support
$test_value = $db->siftDown($test_value); //preventing harmful inputs
$something_testy_else = $db->siftDown($something_testy_else);
$db->query("SELECT * FROM test_table WHERE test_field = '$test_value' AND second_test_field = '$something_testy_else' LIMIT 1");
if($db->countRows()==1)
$dbdata = $db->loadRows(); //returns a numeric/associative array as the result (MYSQL_BOTH)
//TODO: To Process $dbdata
$db->disconnect();
?>

本文来自:http://www.q1010.com/173/504-0.html

注:关于php mysql数据库操作类的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:数据库操作

您可能感兴趣的文章

  • php 数据库操作基础类DBObject Class的用法示例
  • PHP MySQL数据库操作类详解
  • PHP MSSQL数据库操作类示例
上一篇:php 使用Gzip压缩JS、CSS代码的简单示例
下一篇:php 获取twitter最新消息功能实例
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。