search
Homephp教程PHP源码简单的PHP MYSQL PDO操作类 mysql.class.php

通过php pdo链接MYSQL数据库,进行简单的数据操作,我的网站[爱玩电脑 aowana.sinaapp.com],目前使用的就是这个类,如果该类更新会继续分享到oschina。

1. [代码]非常简单的PHP通过PDO操作MYSQL类 mysql.class.php    

<?php
/**
 * MySQL操作类
 * http://www.php.cn/
 * 日期:2015-04-01
 */
class mysql
{
	public $pdo = null;
	public $results = null;

	public function find($sql, $array=array())
	{
		$ok = $this->process($sql, $array);
		if ($ok)
		{
			$this->results->setFetchMode(PDO::FETCH_ASSOC);
			$data = $this->results->fetch();
			return $data;
		}
		else
		{
			return false;
		}
	}

	public function finds($sql, $array=array())
	{
		$ok = $this->process($sql, $array);
		if ($ok)
		{
			$this->results->setFetchMode(PDO::FETCH_ASSOC);
			$data = $this->results->fetchAll();
			return $data;
		}
		else
		{
			return array();
		}
	}

	public function update($sql, $array=array())
	{
		$ok = $this->process($sql, $array);
		if ($ok === false)
			return -1;//执行出错返回-1
		else if ($ok)
			return $this->results->rowCount();
		else
			return 0;
	}

	public function insert($sql, $array=array())
	{
		$ok = $this->process($sql, $array);
		if ($ok)
		{
			$id = $this->pdo->lastInsertId();
			$id = $id ? $id : 1;
			return $id;
		}
		else
		{
			return false;
		}
	}

	public function delete($sql, $array=array())
	{
		$ok = $this->process($sql, $array);
		if ($ok === false)
			return -1;//执行出错返回-1
		else if ($ok)
			return $this->results->rowCount();
		else
			return 0;
	}

	public function query($sql, $array=array())
	{
		return $this->process($sql, $array);
	}

	private function process($sql, $array)
	{
		if (is_null($this->pdo))
			$this->connect();
		$this->results = $this->pdo->prepare($sql);
		//print_r($this->pdo->errorInfo());
		//print_r($this->results->errorInfo());
		return $this->results->execute($array);
	}

	private function connect()
	{
		try
		{
			$this->pdo = new PDO(&#39;mysql:host=&#39; . MYSQL_HOST . &#39;;port=&#39; . MYSQL_PORT . &#39;;dbname=&#39; . MYSQL_DATABASE . &#39;;charset=utf8&#39;, MYSQL_USERNAME, MYSQL_PASSWORD);
		}
		catch (PDOException $error)
		{
			$html = $error->getMessage();
			//SAE Mail to Master
			http503();
		}
	}
}

                   

                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use