php用什么操作系统,PHP 常 用 程 序 参 考(UNIX操作系统)

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

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中有表如下:
";//the code below start tolist all the tables in the database";

echo "

while ($i

$tb_names[$i] = mysql_tablename ($result, $i);

echo "

$tb_names[$i]/n";

$i++;

}

echo "

";

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

?>

3、数据库查询文件:selectdb.php(数据库查询,对结果的显示 )

//演示如何查询数据库

include("conn.php");

/* 执行 SQL 查询 */

$query = "SELECT * FROM my_table";

$result = mysql_query($query) or die("Query failed");

/* 在 HTML 中打印结果 */

print "

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

print "/t

/n";

foreach ($line as $col_value) {

print "/t/t

$col_value/n";

}

print "/t

/n";

}

print "

/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("删除文件失败");}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值