<?php
class
mysql {
private
$db_host
;
private
$db_user
;
private
$db_pwd
;
private
$db_database
;
private
$conn
;
private
$result
;
private
$sql
;
private
$row
;
private
$coding
;
private
$bulletin
= true;
private
$show_error
= false;
private
$is_error
= false;
public
function
__construct(
$db_host
,
$db_user
,
$db_pwd
,
$db_database
,
$conn
,
$coding
) {
$this
->db_host =
$db_host
;
$this
->db_user =
$db_user
;
$this
->db_pwd =
$db_pwd
;
$this
->db_database =
$db_database
;
$this
->conn =
$conn
;
$this
->coding =
$coding
;
$this
->connect();
}
public
function
connect() {
if
(
$this
->conn ==
"pconn"
) {
$this
->conn = mysql_pconnect(
$this
->db_host,
$this
->db_user,
$this
->db_pwd);
}
else
{
$this
->conn = mysql_connect(
$this
->db_host,
$this
->db_user,
$this
->db_pwd);
}
if
(!mysql_select_db(
$this
->db_database,
$this
->conn)) {
if
(
$this
->show_error) {
$this
->show_error(
"数据库不可用:"
,
$this
->db_database);
}
}
mysql_query(
"SET NAMES $this->coding"
);
}
public
function
query(
$sql
) {
if
(
$sql
==
""
) {
$this
->show_error(
"SQL语句错误:"
,
"SQL查询语句为空"
);
}
$this
->sql =
$sql
;
$result
= mysql_query(
$this
->sql,
$this
->conn);
if
(!
$result
) {
if
(
$this
->show_error) {
$this
->show_error(
"错误SQL语句:"
,
$this
->sql);
}
}
else
{
$this
->result =
$result
;
}
return
$this
->result;
}
public
function
create_database(
$database_name
) {
$database
=
$database_name
;
$sqlDatabase
=
'create database '
.
$database
;
$this
->query(
$sqlDatabase
);
}
public
function
show_databases() {
$this
->query(
"show databases"
);
echo
"现有数据库:"
.
$amount
=
$this
->db_num_rows(
$rs
);
echo
"<br />"
;
$i
= 1;
while
(
$row
=
$this
->fetch_array(
$rs
)) {
echo
"$i $row[Database]"
;
echo
"<br />"
;
$i
++;
}
}
public
function
databases() {
$rsPtr
= mysql_list_dbs(
$this
->conn);
$i
= 0;
$cnt
= mysql_num_rows(
$rsPtr
);
while
(
$i
<
$cnt
) {
$rs
[] = mysql_db_name(
$rsPtr
,
$i
);
$i
++;
}
return
$rs
;
}
public
function
show_tables(
$database_name
) {
$this
->query(
"show tables"
);
echo
"现有数据库:"
.
$amount
=
$this
->db_num_rows(
$rs
);
echo
"<br />"
;
$i
= 1;
while
(
$row
=
$this
->fetch_array(
$rs
)) {
$columnName
=
"Tables_in_"
.
$database_name
;
echo
"$i $row[$columnName]"
;
echo
"<br />"
;
$i
++;
}
}
public
function
mysql_result_li() {
return
mysql_result(
$str
);
}
public
function
fetch_array(
$resultt
=
""
) {
if
(
$resultt
<>
""
){
return
mysql_fetch_array(
$resultt
);
}
else
{
return
mysql_fetch_array(
$this
->result);
}
}
public
function
fetch_assoc() {
return
mysql_fetch_assoc(
$this
->result);
}
public
function
fetch_row() {
return
mysql_fetch_row(
$this
->result);
}
public
function
fetch_Object() {
return
mysql_fetch_object(
$this
->result);
}
public
function
findall(
$table
) {
$this
->query(
"SELECT * FROM $table"
);
}
public
function
select(
$table
,
$columnName
=
"*"
,
$condition
=
''
,
$debug
=
''
) {
$condition
=
$condition
?
' Where '
.
$condition
: NULL;
if
(
$debug
) {
echo
"SELECT $columnName FROM $table $condition"
;
}
else
{
$this
->query(
"SELECT $columnName FROM $table $condition"
);
}
}
public
function
delete
(
$table
,
$condition
,
$url
=
''
) {
if
(
$this
->query(
"DELETE FROM $table WHERE $condition"
)) {
if
(!
empty
(
$url
))
$this
->Get_admin_msg(
$url
,
'删除成功!'
);
}
}
public
function
insert(
$table
,
$columnName
,
$value
,
$url
=
''
) {
if
(
$this
->query(
"INSERT INTO $table ($columnName) VALUES ($value)"
)) {
if
(!
empty
(
$url
))
$this
->Get_admin_msg(
$url
,
'添加成功!'
);
}
}
public
function
update(
$table
,
$mod_content
,
$condition
,
$url
=
''
) {
if
(
$this
->query(
"UPDATE $table SET $mod_content WHERE $condition"
)) {
if
(!
empty
(
$url
))
$this
->Get_admin_msg(
$url
);
}
}
public
function
insert_id() {
return
mysql_insert_id();
}
public
function
db_data_seek(
$id
) {
if
(
$id
> 0) {
$id
=
$id
-1;
}
if
(!@ mysql_data_seek(
$this
->result,
$id
)) {
$this
->show_error(
"SQL语句有误:"
,
"指定的数据为空"
);
}
return
$this
->result;
}
public
function
db_num_rows() {
if
(
$this
->result == null) {
if
(
$this
->show_error) {
$this
->show_error(
"SQL语句错误"
,
"暂时为空,没有任何内容!"
);
}
}
else
{
return
mysql_num_rows(
$this
->result);
}
}
public
function
db_affected_rows() {
return
mysql_affected_rows();
}
public
function
show_error(
$message
=
""
,
$sql
=
""
) {
if
(!
$sql
) {
echo
"<font color='red'>"
.
$message
.
"</font>"
;
echo
"<br />"
;
}
else
{
echo
"<fieldset>"
;
echo
"<legend>错误信息提示:</legend><br />"
;
echo
"<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>"
;
echo
"<div style='height:20px; background:#000000; border:1px #000000 solid'>"
;
echo
"<font color='white'>错误号:12142</font>"
;
echo
"</div><br />"
;
echo
"错误原因:"
. mysql_error() .
"<br /><br />"
;
echo
"<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>"
;
echo
"<font color='white'>"
.
$message
.
"</font>"
;
echo
"</div>"
;
echo
"<font color='red'><pre class="
brush:php;toolbar:false
">"
.
$sql
. "