惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
D
Docker
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
The Cloudflare Blog
雷峰网
雷峰网
A
About on SuperTechFans
小众软件
小众软件
博客园 - Franky
博客园 - 聂微东
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
Engineering at Meta
Engineering at Meta
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
量子位
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
罗磊的独立博客
Martin Fowler
Martin Fowler
D
DataBreaches.Net
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Secure Thoughts
Project Zero
Project Zero
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
Security Latest
Security Latest
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks

博客园 - feenan

AngularJS 源码分析3 AngularJS 源码分析2 AngularJS 源码分析1 javascript 中关于对象转换数字值的一些特点 转 CSS hack:针对IE6,IE7,firefox显示不同效果 CSS中一些不经意的细节问题1 CSS关于元素垂直居中的问题 (转)雅虎WEB前端网站优化 -- 34条军规 Js中的一个日期处理格式化函数 Underscore.js 1.3.3 源码分析收藏 Backbone.js 0.9.2 源码分析收藏 Nodejs实现的一个静态服务器例子 Javascript MVC学习杂记3 Javascript MVC学习杂记2 Javascript MVC学习杂记1 C语言实现的一个简单的HTTP程序 C语言string.h中常用字符函数介绍 通过日期生成星期几 Javascript 模块化编程
一个比较常用的关于php下的mysql数据操作类
feenan · 2013-10-30 · via 博客园 - feenan
<?php
/*************************************************************
MySql类封装:
首先连接数据库,需要有参数
参数如何传?
    1、可以用配置文件
    2、可以通过构造函数传参
**************************************************************/
class SqlHelper{
    private $host;
    private $user;
    private $pwd;
    private $dbName;
    private $charset;
    private $conn = null; //保存连接资源

    public function __construct(){
        $this->host  = 'localhost';
        $this->user = 'xuwm';
        $this->pwd = 'bW7LA2pMDAEtnVB7';
        $this->dbName = 'gamejj';
        $this->charset='utf8';
        //连接
        $this->connect($this->host, $this->user, $this->pwd);
        //选库
        $this->switchDb($this->dbName);
        //设置字符集
        $this->setChar($this->charset);
    }

    //负责连接
    private function connect($host, $user, $pwd){
        $conn = mysql_connect($host, $user, $pwd);
         if (!$conn) {
            echo "Unable to connect to DB: " . mysql_error();
            exit;
        }
        $this->conn = $conn;
    }

    //选库
    public function switchDb($db){
        $sql = 'use ' . $db; //注意user 和 ' 有一个空格
        $this->query($sql);
    }

    //设置字符集
    public function setChar($char){
        $sql = 'set names ' . $char;
        $this->query($sql);
    }

    //关闭连接
    public function close(){
        mysql_close($this->conn);
    }

    //负责发送sql查询
    public  function query($sql){
        $result = mysql_query($sql, $this->conn);
        return $result;
    }

    //获取多行多列的select结果
    public function getAll($sql){
        $list  = array();
        $result = $this->query($sql);
        if(!$result)
            return false;
        while($row= mysql_fetch_assoc($result)){
            $list[] = $row;
        }
        return $list;
    }

    //获取一行数据 常用于 聚合函数
    public function getRow($sql){
        $result = $this->query($sql);
        if(!$result)
            return false;
        $row= mysql_fetch_assoc($result);
        return $row;
    }

     //获取一个值
    public function getOne($sql){
        $result = $this->query($sql);
        if(!$result)
            return false;
        $row= mysql_fetch_row($result);
        return $row[0];
    }

}

//$mysql = new SqlHelper();
/*
var_dump($mysql);
$sql = "insert goods values(1121, 4, '苹果')";
$mysql->query($sql);

$sql = 'select * from goods';
$list = $mysql->getAll($sql);
var_dump($list);


$sql = 'select * from goods where goods_id=4';
$list = $mysql->getRow($sql);
var_dump($list);


$sql = 'select count(*) from goods';
$list = $mysql->getOne($sql);
var_dump($list);
*/

posted @ 2013-10-30 13:58  feenan  阅读(390)  评论()    收藏  举报