php连接mysql的类
① 谁能提供一个可用的,注解完全的php连接mysql数据库的类
<?php
/*这个基本上就是我使用的数据库类*/
//屏蔽未定义错误
error_reporting(7);
class DB_MySQL {
var $servername="localhost";
var $dbname="DBASE";
var $dbusername = "ROOT";
var $dbpassword = "";
var $conn = 0;
var $technicalemail='java@cu165.com';
function geterrdesc() {
$this->error = @mysql_error($this->conn);
return $this->error;
}
function geterrno() {
$this->errno = @mysql_errno($this->conn);
return $this->errno;
}
function query($query_string) {
// $this->result = mysql_db_query($this->dbname,$query_string);
$this->result = mysql_query($query_string);
if (!$this->result) {
$this->halt("SQL 无效: ".$query_string);
}
return $this->result;
}
function num_rows($queryid) {
$this->rows = mysql_num_rows($queryid);
if (empty($queryid)){
$this->halt("Query id 无效:".$queryid);
}
return $this->rows;
}
function fetch_array($queryid) {
$this->record = mysql_fetch_array($queryid);
if (empty($queryid)){
$this->halt("Query id 无效:".$queryid);
}
return $this->record;
}
function conn(){
$this->conn = mysql_connect($this->servername, $this->dbusername, $this->dbpassword) or die(mysql_error("数据库链接失败"));
return $this->conn;
}
function selectdb(){
if(!mysql_select_db($this->dbname)){
$this->halt("数据库链接失败");
}
}
function my_close() {
// mysql_close($this->conn);
mysql_close();
}
function fetch_row($queryid) {
$this->record = mysql_fetch_row($queryid);
if (empty($queryid)){
$this->halt("queryid 无效:".$queryid);
}
return $this->record;
}
function fetch_one_num($query) {
$this->result = $this->query($query);
$this->record = $this->num_rows($this->result);
if (empty($query)){
$this->halt("Query id 无效:".$query);
}
return $this->record;
}
function fetch_one_array($query) {
$this->result = $this->query($query);
$this->record = $this->fetch_array($this->result);
if (empty($query)){
$this->halt("Query id 无效:".$query);
}
return $this->record;
}
function free_result($query){
if (!mysql_free_result($query)){
$this->halt("fail to mysql_free_result");
}
}
function insert_id(){
$this->insertid = mysql_insert_id();
if (!$this->insertid){
$this->halt("fail to get mysql_insert_id");
}
return $this->insertid;
}
/*========================================================================*/
// Create an array from a multidimensional array returning formatted
// strings ready to use in an Insert query, saves having to manually format
// the (Insert INTO table) ('field', 'field', 'field') VALUES ('val', 'val')
/*========================================================================*/
function compile_db_insert_string($data) {
$field_names = "";
$field_values = "";
foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "\\'", $v );
//$v = preg_replace( "/#/", "\\#", $v );
$field_names .= "$k,";
$field_values .= "'$v',";
}
$field_names = preg_replace( "/,$/" , "" , $field_names );
$field_values = preg_replace( "/,$/" , "" , $field_values );
return array( 'FIELD_NAMES' => $field_names,
'FIELD_VALUES' => $field_values,
);
}
/*========================================================================*/
// Create an array from a multidimensional array returning a formatted
// string ready to use in an Update query, saves having to manually format
// the FIELD='val', FIELD='val', FIELD='val'
/*========================================================================*/
function compile_db_update_string($data) {
$return_string = "";
foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "\\'", $v );
$return_string .= $k . "='".$v."',";
}
$return_string = preg_replace( "/,$/" , "" , $return_string );
return $return_string;
}
function halt($msg){
global $technicalemail,$debug;
$message = "<html>\n<head>\n";
$message .= "<meta content=\"text/html; charset=gb2312\" http-equiv=\"Content-Type\">\n";
$message .= "<STYLE TYPE=\"text/css\">\n";
$message .= "<!--\n";
$message .= "body,td,p,pre {\n";
$message .= "font-family : Verdana, Arial, Helvetica, sans-serif;font-size : 12px;\n";
$message .= "}\n";
$message .= "</STYLE>\n";
$message .= "</head>\n";
$message .= "<body bgcolor=\"#EEEEEE\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";
$message .= "<font size=10><b>系统调试</b></font><font size=6><b>(by 大白菜芯 )</b></font>\n<hr NOSHADE SIZE=1>\n";
$content = "<p>数据库出错:</p><pre><b>".htmlspecialchars($msg)."</b></pre>\n";
$content .= "<b>Mysql error description</b>: ".$this->geterrdesc()."\n<br>";
$content .= "<b>Mysql error number</b>: ".$this->geterrno()."\n<br>";
$content .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br>";
$content .= "<b>Script</b>: http://".$_SERVER[HTTP_HOST].getenv("REQUEST_URI")."\n<br>";
$content .= "<b>Referer</b>: ".getenv("HTTP_REFERER")."\n<br><br>";
$message .= $content;
$message .= "<p>请尝试刷新你的浏览器,如果仍然无法正常显示,请联系<a href=\"technicalemail."\'>mailto:".$this->technicalemail."\">管理员</a>.</p>";
$message .= "</body>\n</html>";
echo $message;
$headers = "From: nt.cn <$this->technicalemail>\r\n";
$content = strip_tags($content);
@mail($technicalemail,"数据库出错",$content,$headers);
exit;
}
}
?>
② PHP连接MYSQL数据库
php连接数据库类,可以使用;
或者自己写个连接代码:
自定义连接地址、用户名、密码、数据库
用$connect= mysql_connect('localhost','user','pwd') or die('连接错误:'.mysql_error());
<?php
$servername='localhost' ;
$database_username='root';
$database_password='password';
$database_name='databasename';
$connect=mysql_connect($servername,$database_username,$database_password);
mysql_query("set names 'utf8'");
mysql_select_db($database_name);
?>
③ PHP mysql操作类的问题
你是想用填入一个数组然后自动解析出SQL语句么?
我给你个思路吧...
SELECT [select options] FROM [tables] [CONDITION]
首先是select options,一般有查询COUNT(*)、*或者指定一些查询值
所以可以把select options的选项定义在一个数组中的一个新的组
也就是
$array = array(
"SELECT" => array("a", "b")
);
你需要循环SELECT的值,然后解析成SQL
解析出来大概就是 SELECT a,b FROM ...
然后table,这个好说.. 直接给个固定值
最麻烦就是后面的CONDITION,也就是 SELECT **** WHERE a = 'a' 之类的东西
这个你可以作为常项
array(
“SELECT” => array("a","b"),
"username" => "mutou"
);
你直接循环这个单一数组,把SELECT单列出来,后面的用else,然后进行key和value的提取,获取值填入SQL
这段解析出来应该是 SELECT a,b FROM table WHERE username = "mutou"
其他SELECT的常用参数还有ORDER,LIMIT等,可以用同样的办法
最近写了一个比较简单的SELECT类.. 所以暂说这么多了
④ 帮忙写一个PHP,连接mysql数据库的一个类,实现连接,执行sql语句就好
class mysql{
private $name;
private $host;
private $pw;
private $table_name;
private $bianma;
function __construct($h,$n,$p,$b){
$this->name=$n;
$this->host=$h;
$this->pw=$p;
$this->conn();
$this->bianma=$b;
$this->bianma();
}
function conn(){
return mysql_connect("$this->host","$this->name","$this->pw");
}
function db($table){
mysql_select_db("$table");
}
function query($sql=""){
return mysql_query("$sql");
}
function bianma(){
mysql_query("set names '$this->bianma'");
}
}
$mysql=new mysql("localhost","root","","GBK");
$mysql->db("mynews");
⑤ php 类中连接mysql
classdemo
{
function__destruct()
{
$DB->close();//$DB哪里来的?应该是$this->DB->close()吧
}
publicfunction__construct()
{
$DB=newDB_MySQL;//这属于函数内部变量,函数执行完就消失了。所以应该用$this->DB=newDB_MySQL
$DB->connect(servername,dbusername,dbpassword,dbname,usepconnect);//同理,需要改成$this->DB,参数也有问题吧,还是你为避免泄露sql账号密码故意这么写的?
}
functiontest()
{
$sql1="SELECT*FROMtablimit1";
$txt=$DB->fetch_one_array($sql1);//同理,需要改成$this->DB
return$txt['id'];
}
}
$person=newdemo;
echo$person->test();
⑥ php在类中怎么连接mysql数据库
classdbmysqli{
private$error='';
private$errno=0;
private$port;
private$host;
private$username;
private$password;
private$dbname;
private$charset;
public $mysqli;
/**
*构造函数
*@authoraaron
*@returnvoid
*/
function__construct(){
$this->port=3306;
$this->host='127.0.0.1';
$this->username='usert';
$this->password="******";
$this->dbname='testdb';
$this->charset='UTF8';
$db=newmysqli($this->host,$this->username,$this->password,$this->dbname,$this->port);
if(mysqli_connect_error()){
$this->error=mysqli_connect_error();
$this->errno=mysqli_connect_errno();
returnFALSE;
}
$db->query("SETNAMES".$this->charset);
$this->mysqli=$db;
}
}
⑦ PHP访问MYSQL数据库封装类(附函数说明)
复制代码
代码如下:
<?php
/*
MYSQL
数据库访问封装类
MYSQL
数据访问方式,php4支持以mysql_开头的过程访问方式,php5开始支持以mysqli_开头的过程和mysqli面向对象
访问方式,本封装类以mysql_封装
数据访问的一般流程:
1,连接数据库
mysql_connect
or
mysql_pconnect
2,选择数据库
mysql_select_db
3,执行SQL查询
mysql_query
4,处理返回的数据
mysql_fetch_array
mysql_num_rows
mysql_fetch_assoc
mysql_fetch_row
etc
*/
class
db_mysql
{
var
$querynum
=
0
;
//当前页面进程查询数据库的次数
var
$dblink
;
//数据库连接资源
//链接数据库
function
connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0
,
$halt=true)
{
$func
=
empty($pconnect)
?
'mysql_connect'
:
'mysql_pconnect'
;
$this->dblink
=
@$func($dbhost,$dbuser,$dbpw)
;
if
($halt
&&
!$this->dblink)
{
$this->halt("无法链接数据库!");
}
//设置查询字符集
mysql_query("SET
character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this->dblink)
;
//选择数据库
$dbname
&&
@mysql_select_db($dbname,$this->dblink)
;
}
//选择数据库
function
select_db($dbname)
{
return
mysql_select_db($dbname,$this->dblink);
}
//执行SQL查询
function
query($sql)
{
$this->querynum++
;
return
mysql_query($sql,$this->dblink)
;
}
//返回最近一次与连接句柄关联的INSERT,UPDATE
或DELETE
查询所影响的记录行数
function
affected_rows()
{
return
mysql_affected_rows($this->dblink)
;
}
//取得结果集中行的数目,只对select查询的结果集有效
function
num_rows($result)
{
return
mysql_num_rows($result)
;
}
//获得单格的查询结果
function
result($result,$row=0)
{
return
mysql_result($result,$row)
;
}
//取得上一步
INSERT
操作产生的
ID,只对表有AUTO_INCREMENT
ID的操作有效
function
insert_id()
{
return
($id
=
mysql_insert_id($this->dblink))
>=
0
?
$id
:
$this->result($this->query("SELECT
last_insert_id()"),
0);
}
//从结果集提取当前行,以数字为key表示的关联数组形式返回
function
fetch_row($result)
{
return
mysql_fetch_row($result)
;
}
//从结果集提取当前行,以字段名为key表示的关联数组形式返回
function
fetch_assoc($result)
{
return
mysql_fetch_assoc($result);
}
//从结果集提取当前行,以字段名和数字为key表示的关联数组形式返回
function
fetch_array($result)
{
return
mysql_fetch_array($result);
}
//关闭链接
function
close()
{
return
mysql_close($this->dblink)
;
}
//输出简单的错误html提示信息并终止程序
function
halt($msg)
{
$message
=
"<html>\n<head>\n"
;
$message
.=
"<meta
content='text/html;charset=gb2312'>\n"
;
$message
.=
"</head>\n"
;
$message
.=
"<body>\n"
;
$message
.=
"数据库出错:".htmlspecialchars($msg)."\n"
;
$message
.=
"</body>\n"
;
$message
.=
"</html>"
;
echo
$message
;
exit
;
}
}
?>
⑧ MySQL与PHP的连接语句类型
PHP与MYSQL数据库连接语句
mysql_connect(”localhost”,
“username”,
“password”)
数据库选择
mysql_select_db(”test”)
⑨ 一个关于PHP连接mysql数据库类的问题
如果你的程序只连接一个MYSQL,那么mysql_connect的返回值是多余的,系统会自动记录,连接成功以后直接mysql_query($sql)就可以了。
⑩ PHP Mysql操作类
<?php
class Mysql{
private $db_host;
private $db_user;
private $db_pass;
private $db_table;
private $ut;
function __construct($db_host,$db_user,$db_pass,$db_table,$ut){
$this->db_host=$db_host;
$this->db_user=$db_user;
$this->db_pass=$db_pass;
$this->db_table=$db_table;
$this->host=$ut;
$this->connect();
}
function connect(){
$conn= mysql_connect($this->db_host,$this->db_user,$this->db_pass) or die("链接错误:".$this->db_table);
mysql_select_db($this->db_table,$conn);
mysql_query("set names '$this->ut'");
}
//执行SQL语句的方法
public function query($sql){
$this->query=mysql_query($sql);
}
//查询一条记录返回一维数组
public function select_once($table,$where=1,$fields="*"){
$sql="select $fields from $table where $where limit 1";
$this->query($sql);
$result=$this->_array();
return $result;
}
//查询多条记录返回二维数组
public function select_all($table,$where=1,$fields="*"){
$sql="select $fields from $table where $where";
$this->query($sql);
$result="";
while($rs=$this->_array()){
$result[]=$rs;
}
return $result;
}
//返回查询记录数
public function getRowsNum($sql) {
$query = $this->query ( $sql );
return mysql_num_rows ( $query );
}
//mysql_fetch_array
private function _array(){
return $rs=mysql_fetch_array($this->query);
}
}
$db=new Mysql('localhost','root','','yugaoyf',"gb2312");
?>
使用实例 同样是使用了 封装
//查询全部结果
public function select_all(){
return $this->db->select_all("message as a,reply as b","a.id = b.mid ");
}
//查询一条记录
public function select_once(){
return $this->db->select_once("message as a,reply as b","a.id = b.mid and a.id='".$_GET['id']."'");
}