常 用 程 序 参 考(UNIX操作系统)(PHP)

来源:互联网 发布:娱乐圈乱 知乎匿名 编辑:程序博客网 时间:2024/05/16 04:40
1、数据库连接文件:conn.php(连接MYSQL数据库的配置文件)
<?
//本文件是数据库连接的配置文件,连接参数在此定义
$host="localhost";
$user="******";//you need modify here,replace your database's account
$passwd="******";//also modify here,replace your database's password
$dbname="******";//and modify here,replace your database's name if(!$link=mysql_connect("$host", "$user", "$pwd")) //start connect your database
{
print 'Could not connect to database';
exit;
}
mysql_select_db("$dbname") or die("Could not select database");
?>
2、数据库列表文件:list_alltb.php(列出数据库中所有表格名称)

<?
//演示了如何列出一个数据库的所有表

include("conn.php");
$result = mysql_list_tables($dbname);
if (!$result) {
print "DB Error, could not list tables/n";
print 'MySQL Error: ' . mysql_error();
exit;
}
$i=0;
echo "数据库$dbname中有表如下:<br>";//the code below start tolist all the tables in the database";
echo "<table border=1>";
while ($i<mysql_num_rows($result)) {
$tb_names[$i] = mysql_tablename ($result, $i);
echo "<tr><td>$tb_names[$i]</td></tr>/n";
$i++;
}
echo "</table>";

mysql_free_result($result);//free the resource at the end
?>

3、数据库查询文件:selectdb.php(数据库查询,对结果的显示 )
<?php
//演示如何查询数据库
include("conn.php");
/* 执行 SQL 查询 */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed");

/* 在 HTML 中打印结果 */
print "<table>/n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "/t<tr>/n";
foreach ($line as $col_value) {
print "/t/t<td>$col_value</td>/n";
}
print "/t</tr>/n";
}
print "</table>/n";

/* 释放资源 */
mysql_free_result($result);

/* 断开连接 */
mysql_close($link);
?>

4、数据库操纵文件:operatedb.php(数据库记录的增加、删除、修改 )
<?
//演示了如何对数据库中的数据进行插入,删除和更新操作

include("conn.php");

$sql="insert into user (ID,PW,Name,Sex,Email,Title,Info) values ('$userid','$userpw','$usernam
e','$usersex','$usermail','$usertitle','$userinfo')";//插入语句
mysql_query($sql) or die(mysql_error());//执行插入操作

$sql="delete from user where ID='$userid'"; //删除语句
mysql_query($sql) or die(mysql_error()); //执行删除操作

$sql="update user set PW='$userpw',Name='$username',Sex='$usersex',Email='$usermail',
Title='$usertitle',Type='$usertype',Info='$userinfo' where ID='$userid'"; //更新语句
mysql_query($sql) or die(mysql_error()); //执行删除操作


mysql_close($link); // 断开连接

?>

5、文件操作程序:fileoperate.php (最常用的文件操作)
<?
$filename="****";//要操作的文件名

//读操作,读出一个文件所有内容到一个字符串变量中
$content=file($filename);
$content=join("",$content);
print $content;

//再将该字符创串的内容写入原来的文件中
if(!$fp=fopen($filename,"w"))//”w“方式打开文件时,如果文件不存在,则创建该文件;如果存在,则覆盖原文件
{
die("open file $filename error!");
}
fputs($fp,$content,strlen($content));
fclose($fp);//写完后要及时关闭文件句柄

//追加到文件末尾
if(!$fp=fopen($filename,"a"))//追加到文件末尾,用"a"方式打开
{
die("open file $filename error!");
}
fputs($fp,$content,$strlen($content));
fclose($fp);

//删除文件
if(is_file($filename))
{
unlink($filename) or die("删除文件失败");
}

原创粉丝点击