/建立连接/
class database{
/初始化数据/
public
iP=”xx”;public
i
P
=
”
x
x
”
;
p
u
b
l
i
c
username=”xx”;
public
psw=”xx”;public
p
s
w
=
”
x
x
”
;
p
u
b
l
i
c
charType=”xx”;
public $selectDb=”xx”;
/连接mysql/
function link_mysql(){
mysql_connect(
this−>iP,
t
h
i
s
−
>
i
P
,
this->username,
this−>psw);mysqlsetcharset(
t
h
i
s
−
>
p
s
w
)
;
m
y
s
q
l
s
e
t
c
h
a
r
s
e
t
(
this->charType);
mysql_select_db($this->selectDb);
}
/构造函数/
function __construct(
a,
a
,
b,
c,
c
,
d,e){
e){
this->iP=
a;
a
;
this->username=
b;
b
;
this->psw=
c;
c
;
this->charType=
d;
d
;
this->selectDb=$e;
}
}
//然后是mysql执行语句:
class sql {
public $sql=”xx”;
/检查语句是否正确如果正确就打印出来/
function sqlword(){
sql1=
s
q
l
1
=
this->sql;
result=mysqlquery(
r
e
s
u
l
t
=
m
y
s
q
l
q
u
e
r
y
(
sql1);
if(result===false){
echo mysql_error();
}
else{
//echo
result===false){ echo mysql_error(); } else{ //echo
_SERVER[‘QUERY_STRING’];
num=mysqlnumfields(
n
u
m
=
m
y
s
q
l
n
u
m
f
i
e
l
d
s
(
result);
echo “
/ 这是表头/
echo ““;
for( i=0; i = 0 ; i< num; n u m ; i++){
fieldName=mysqlfieldname( f i e l d N a m e = m y s q l f i e l d n a m e ( result,$i);
echo ““;
}
echo ““;
/ 这是数据库信息/
while( re=mysqlfetcharray( r e = m y s q l f e t c h a r r a y ( result)){
echo ““;
for( i=0; i = 0 ; i< num; n u m ; i++){
fieldName=mysqlfieldname( f i e l d N a m e = m y s q l f i e l d n a m e ( result,$i);
echo ““;
}
echo ““;
}
echo “
“.$fieldName.” |
“. re[ r e [ fieldName].” |
}
}
/ 构造函数/
function __construct( g){
//parent::__construct(); g){ //parent::__construct(); this->sql=$g;
}
}
/*mysql_num_fields 是取得结果集中字段的数目 用法:mysql_num_fields ( result)mysqlfieldname是取得结果中指定字段的字段名用法:mysqlfieldname( r e s u l t ) m y s q l f i e l d n a m e 是 取 得 结 果 中 指 定 字 段 的 字 段 名 用 法 : m y s q l f i e l d n a m e ( result , fieldindex)mysqlfetcharray是从结果集中取得一行作为关联数组用法:mysqlnumfields( f i e l d i n d e x ) m y s q l f e t c h a r r a y 是 从 结 果 集 中 取 得 一 行 作 为 关 联 数 组 用 法 : m y s q l n u m f i e l d s ( result )*/
最后是建立对象:
/连接/
final=newdatabase(“localhost”,”root”,”123”,”utf8”,”task04”);
f
i
n
a
l
=
n
e
w
d
a
t
a
b
a
s
e
(
“
l
o
c
a
l
h
o
s
t
”
,
”
r
o
o
t
”
,
”
123
”
,
”
u
t
f
8
”
,
”
t
a
s
k
04
”
)
;
final->link_mysql();
/选择数据库/
new sql(“use task04”);
/对数据库里面的文件进行操作/
$final2=new sql(“select * from list”);
/打印在页面上/
echo $final2->sqlword();