这是一款简单实用的php连接mysql数据库类了,只做了数据查询,返回数组集,获取新的id 取得记录行等简单的mysql数据库连接代码。
代码如下 |
复制代码 |
class database {
var $dblink;
function connect($dbhost, $dbuser, $dbpw, $dbname = "") {
$this->dblink = mysql_connect($dbhost, $dbuser, $dbpw);
mysql_query("set names 'utf8'");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
if($dbname) {
mysql_select_db($dbname, $this->dblink);
}
}
function query($sql) {
$result = mysql_query($sql, $this->dblink);
return $result;
}
function fetch_array($result) {
return mysql_fetch_array($result);
}
function insert_id() {
$id = mysql_insert_id();
return $id;
}
function getrow($sql) {
$result = mysql_query($sql, $this->dblink);
return mysql_fetch_assoc($result);
}
function getdetailrow($sql) {
$result = mysql_query($sql, $this->dblink);
return mysql_fetch_array($result);
}
//www.111cn.net
function close() {
mysql_close($this->dblink);
}
} |
//调用 方法
代码如下 |
复制代码 |
$db = new database;
$db->connect($dbhost, $dbuser, $dbpw, $dbname);
unset($dbhost, $dbuser, $dbpw, $dbname); |
?>
这是一款我在自己用时写的一款利用php 导出mysql数据库的数据保存成csv文件,并提供下载,原理很简单就是把mysql数据查出来,然后以csv格式保存到一个.csv文件这样就OK了。
$times = time();
$filename = $times.".csv";
$a = "联系人,联系电话,申请时间 ";
$days = postget("days");
$mktime = daystomktime($days);
$sql = "select * from v_tugou where ($times-times)<$mktime";
$db = new db();
$result = $db->query( $sql );
$rs = $db->fetch($result,0);
foreach($rs as $v=>$vv)
{
$a.=$vv['name'].','. $vv['mo'].",";
$a.=date('y-m-d ',$vv['times'])." ";
}
//echo $a;
$hod = fopen ($filename,"w+");
if( fwrite($hod,$a) )
{
echo "生成excel文件成功,点击<a href=$filename target=_blank>右击另存为excel文档</a>";
}
/*
数据结构
--
-- 表的结构 `hn_tugou`
--
create table if not exists `v_tugou` (
`id` int(4) not null auto_increment,
`name` varchar(20) not null default '0',
`mo` varchar(20) not null default '0',
`times` int(4) default null,
`ip` varchar(20) not null default '0',
primary key (`id`)
) engine=myisam default charset=utf8 auto_increment=2 ;
--
-- 导出表中的数据 `hn_tugou`
--
insert into `hn_tugou` (`id`, `name`, `mo`, `times`, `ip`) values
(1, 'adsense', 'www.111cn.net', 1283241159, '192.168.0.119');
本站原创文章转载注明来自http://www.111cn.net/phper/php.html
*/
这里不是php与mysql的留言板程序,而是一款简单实用的php access 留言板程序哦,利用了adodb来连接access数据库哦。
*/
代码如下 |
复制代码 |
error_reporting(0);
$conn = new com("adodb.connection");
$conn->open("driver={microsoft access driver (*.mdb)}; dbq=" . realpath("db.mdb "));
$rs=new com("adodb.recordset");
$abc = $_get["abc"]; $webn = $_post["webn"]; $name = $_post["name"]; $pws = $_post["pws"]; $newpws = $_post["newpws"];
$rs->open("select * from [web]",$conn,1,1);
$adminname = $rs->fields(1)->value;
$adminpws = $rs->fields(2)->value;
$ll = $rs->fields(3)->value;
$webname = $rs->fields(4)->value;
$rs->close();
$admincookie = $adminname;
$conn->execute("update [web] set [ll] = '$ll'+1"); //$rs->close();
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title><? echo $webname ?></title>
<style type="text/css教程">
<!--
body {font-size:14px; line-height:25px; margin:10px auto; padding:10px; width:680px; border:solid #8aa 1px;}
a {color:#008; text-decoration:none;}
a:hover {color:#f00; text-decoration:underline;}
hr {color:#8aa; height:1px;}
-->
</style>
</head>
<body>
<?
if ($abc=="admin"){
if ($name==$adminname && $pws==$adminpws){
setcookie("admin", $admincookie, time()+3600); header("location:./");
}else{echo "<script>alert('用户名或密码错误!');history.back();</script>";}
}
if ($abc=="exit"){
setcookie("admin", "", time()-3600); header("location:./");
}
if ($abc=="editadmin"){
if (!$webname || !$name || !$pws){
echo "<script>alert('留言本、用户名、原密码不能为空!');history.back();</script>";
}
if ($pws!=$adminpws){
echo "<script>alert('愿密码错误!');history.back();</script>";
}
if($newpws!=""){
$conn->execute("update [web] set [webname] = '$webname' , [name] = '$name' , [pws] = '$newpws'");
$conn->close(); //$conn=null;
echo "<script>alert('修改成功,请重新登录!');location='?abc=exit';</script>";
}else{
$conn->execute("update [web] set [webname] = '$webn' , [name] = '$name'");
$conn->close(); //$conn=null;
echo "<script>alert('修改成功!');location='./';</script>";
}
}
?>
<b>【 <a href="./"><? echo $webname ?></a> 】</b>
<a href="">管理</a> <? if (isset($_cookie["admin"])){ ?> <a href="">修改资料</a> <a href="">安全退出</a><? ;} ?>
<? if ($abc=="user" && !isset($_cookie["admin"])){ ?>
<form method="post" action="">
用户名:<input name="name" type="text" value=""/>
密码:<input name="pws" type="password" value=""/>
<input name="add" type="submit" value="确定" />
</form>
<? ;} ?>
<? if ($abc=="edituser"){ ?>
<form method="post" action="">
留言本:<input name="webn" type="text" value="<? echo $webname; ?>" size="80"/>
<br />
用户名:<input name="name" type="text" value="<? echo $admincookie; ?>"/>
原密码:<input name="pws" type="password" value=""/>
新密码:<input name="newpws" type="password" value=""/>
<input name="add" type="submit" value="确定" />
</form>
<? ;} ?>
<hr/>
<?
$id=$_get['id'];
$sj=date('y-m-d');
if($abc=="add"){
$nr=$_post['nr'];
$ip=getenv("remote_addr");
if (!$nr){
echo "<script>alert('留言不能为空!');history.back();</script>";
}else{
$conn->execute("insert into [txt] (nr,sj) values ('$nr','$sj')");
}
echo "<script>alert('操作成功!');location='./';</script>";
$conn->close(); $conn=null;
}
if($abc=="del"){
$conn->execute("delete from [txt] where [id]=".$id);
$conn->close(); $conn=null; header("location:./");
}
if($abc=="edit"){
$nr=$_post['nr'];
$hf=$_post['hf'];
$conn->execute("update [txt] set [nr] = '$nr' , [hf] = '$hf' where [id]=".$id);
$conn->close(); $conn=null; header("location:./");
}
$zd=$_get['zd'];
if($zd!=""){
if($zd=="0")$zd=1; else $zd=0;
$conn->execute("update [txt] set [zd] = '$zd' where [id]=".$id);
$conn->close(); $conn=null; header("location:./");
}
?>
<center>
<form method="post" action="">
<textarea name="nr" cols="88" rows="8" style="line-height:25px;"></textarea><br /><br />
<input name="add" type="submit" value="发表留言" />
</form>
</center>
<hr/>
<?
//分页
$rs->open("select * from [txt] order by [zd] desc, [sj] desc",$conn,1,3);
$rs->pagesize=5;
$page=trim($_get['page']);
if($page=="" || is_numeric(intval($page))<=0){$page=1;}else if(intval($page)>$rs->pagecount){$page=$rs->pagecount;}
$page=intval($page);
if(!$rs->eof||$rs->bof){
$rs->absolutepage=$page; $mypagesize=$rs->pagesize; $i=$rs->recordcount()-($page-1)*$rs->pagesize;
while(!$rs->eof && $mypagesize>0 && $i>=$rs->recordcount()-1-$rs->pagesize*$page){
$id = $rs->fields(0)->value;
$nr = $rs->fields(1)->value;
$hf = $rs->fields(2)->value;
$sj = $rs->fields(3)->value;
$zd = $rs->fields(4)->value;
?>
<div style="border:#eee solid 1px; background:#f8f8f8; padding:0 10px;">
<div style="float:left;">
<? if ($zd!=0) echo "【 <font color='#008800'>置顶</font> 】"; else echo "id.".$i; ?>
<? if ($_cookie["admin"]==$admincookie){ ?>
操作:<a href=''><? if ($zd==0) echo "置顶"; else echo "取消置顶"; ?></a> |
<a href=''>修改/回复</a> |
<a href=''>删除</a>
<? ;} ?>
</div>
<div style="float:right;">
date:<? echo $sj; ?>
</div>
<div style="clear:both"></div>
</div>
<hr/>
<? if ($abc=="hf" && $id==$_get['id']){ ?>
<form method="post" action="" id="<? echo $id ?>">
留言:<textarea name="nr" cols="80" rows="8" style="line-height:25px;"><? echo $nr; ?></textarea><br />
回复:<textarea name="hf" cols="80" rows="8" style="line-height:25px;"><? echo $hf; ?></textarea><br />
操作:<input name="add" type="submit" value="回复/修改" />
</form>
<? ;} ?>
<div style="margin:20px;">
<?
$nr = str_replace(" "," ".'<br/>',$nr);
$nr = str_replace(" ",' ',$nr);
echo $nr."<br />";
?>
<?
if ($hf!=""){
echo "<font color='#aa0000'>回复:";
$hf = str_replace(" "," ".'<br/>',$hf);
$hf = str_replace(" ",' ',$hf);
echo $hf."</font>";
}
?>
</div>
<hr/>
<? $rs->movenext; $mypagesize--; $i--; } } //分页循环 ?>
<center>
留言数(<? echo $rs->recordcount(); ?>)
<a href="">首页</a>
<a href="">上页</a>
第<? echo $page;?>/<? echo $rs->pagecount;?>页
<a href="">下页</a>
<a href="">尾页</a>
访问量(<? echo $ll ?>)
</center>
<? $rs->close(); $rs=null; $conn->close(); $conn=null; ?>
</body>
</html>
|
数据库结构
id name pws ll webname
1 abc abc 4 abc 留言本 php+acc
代码如下 |
复制代码 |
$qh=mysql_query("select count(*) as rcnt from table where your_condition_here order by whatever");
$data=mysql_fetch_array($qh);
$nr=$data["rcnt"];
//判断偏移量参数是否传递给了脚本,如果没有就使用默认值0
if (empty($offset))
{
$offset=0;
}
//查询结果(这里是每页20条,但你自己完全可以改变它)
$result=mysql_query("select id,name,phone from table where your_condition_here order by whatever limit $offset, 20");
//显示返回的20条记录
while ($data=mysql_fetch_array($result))
{
//换成你用于显示返回记录的代码
}
//下一步,要写出到其它页面的链接
if(!$offset) //如果偏移量是0,不显示前一页的链接
{
$preoffset=$offset-20;
print "<a href="$php_self?offset=$preoffset">前一页</a> ";
}
//计算总共需要的页数
$pages=ceil($nr/20); //$pages变量现在包含所需的页数
for ($i=1; $i <= $pages; $i++)
{
$newoffset=20*$i;
print "<a href="$php_self?offset=$newoffset">$i</a> ";
}
//检查是否是最后一页
if ($pages!=0 && ($newoffset/20)!=$pages)
{
print "<a href="$php_self?offset=$newoffset">下一页</a> ";
}
|
本文章是也一款入门级php教程了,主要是告诉你如何利用php mysq数据编辑更新哦。
代码如下 |
复制代码 |
$db = mysql教程_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$db);
// 如果提交了submit按钮
if ($submit) {
// 如果没有id,则是在增加记录,否则是在修改记录
if ($id) {
$sql = "update employees set first='$first',last='$last', address='$address',position='$position' where id=$id";
}
else {
$sql = "insert into employees (first,last,address,position) values ('$first','$last','$address','$position')";
}
// 向数据库教程发出sql命令
$result = mysql_query($sql);
echo "记录修改成功!<>";
echo "<a href='$php_self'>返回</a>";
}
elseif ($delete) {
// 删除一条记录
$sql = "delete from employees where id=$id";
$result = mysql_query($sql);
echo "记录删除成功!<>";
echo "<a href='$php_self'>返回</a>";
}
else {
// 如果还没有按submit按钮,那么执行下面这部分程序
if (!$id) {
// 如果不是修改状态,则显示员工列表
$result = mysql_query("select * from employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href="%s?id=%s">%s %s</a> ",
$php_self, $myrow["id"], $myrow["first"], $myrow["last"]);
printf("<a href="%s?id=%s&delete=yes">(delete)</a><br>", $php_self, $myrow["id"]);
}
}
?>
<a href="<?php echo $php_self?>">返回</a>
<form method="post" action="<?php echo $php_self?>">
<?php
if ($id) {
// 是在编辑修改状态,因些选择一条记录
$sql = "select * from employees where id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$first = $myrow["first"];
$last = $myrow["last"];
$address = $myrow["address"];
$position = $myrow["position"];
// 显示id,供用户编辑修改
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php
}
?>
名:<input type="text" name="first" value="<?php echo $first ?>">
姓:<input type="text" name="last" value="<?php echo $last ?>">
<br>
住址:<input type="text" name="address" value="<?php echo $address ?>">
职位:<input type="text" name="position" value="<?php echo $position ?>">
<br>
<input type="submit" name="submit" value="输入信息">
</form>
<?php
}
?>
</body>
</html>
|