常 用 程 序 參 考(UNIX操作系統)(PHP)

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("刪除文件失敗");
}

發佈了96 篇原創文章 · 獲贊 0 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章