<?php
require
(
"./config.php"
);
class
Model{
private
$link
;
public
function
__construct(){
$this
-> link = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATANAME)
or
die
(
"数据库连接失败"
);
mysqli_set_charset(
$this
-> link,
"utf8"
);
}
public
function
find(
$table
=
""
,
$key
=
""
,
$value
=
""
){
if
(!
$key
||!
$value
){
$sql
=
"select * from {$table}"
;
}
else
{
$sql
=
"select * from {$table} where {$key} = '{$value}'"
;
}
$res
= mysqli_query(
$this
-> link,
$sql
);
$arr
= mysqli_fetch_all(
$res
,MYSQLI_ASSOC);
mysqli_free_result(
$res
);
return
$arr
;
}
public
function
ins(
$table
=
""
,
$zd
=
"name,score"
,
$value
=
""
){
$arr
=
explode
(
","
,
$value
);
$str
=
""
;
foreach
(
$arr
as
$k
=>
$v
){
$str
.=
"'"
.
$v
.
"'"
.
","
;
}
$str
= rtrim(
$str
,
","
);
$sql
=
"insert into {$table}({$zd})values({$str})"
;
$res
= mysqli_query(
$this
-> link,
$sql
);
return
mysqli_insert_id(
$this
-> link);
}
public
function
upd(
$table
=
""
,
$key
=
""
,
$value
=
""
,
$key2
=
""
,
$value2
=
""
){
$sql
=
"update {$table} set {$key}='{$value}' where {$key2}='{$value2}'"
;
$res
= mysqli_query(
$this
-> link,
$sql
);
return
mysqli_affected_rows(
$this
-> link);
}
public
function
del(
$table
=
""
,
$key
=
""
,
$value
=
""
){
$sql
=
"delete from {$table} where {$key}='{$value}'"
;
$res
= mysqli_query(
$this
-> link,
$sql
);
return
mysqli_affected_rows(
$this
-> link);
}
public
function
__destruct(){
if
(isset(
$res
))
mysqli_free_result(
$res
);
mysqli_close(
$this
-> link);
}
}
$m
=
new
Model();
var_dump(
$m
-> ins(
"stu"
,
"name"
,
"zhu"
));
?>