代碼
<?php
//1.mysql_pconnect()...串連資料庫
$conn= mysql_connect("localhost", "root", "120455637") or die("Could not connect: " . mysql_error());
//解決亂碼問題: @mysql_query("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary",$conn); //2.mysql_select_db()...選擇資料表
mysql_select_db('wjbtest',$conn);
$sql="select * from users";
//3.mysql_query()...執行sql語言
$query=mysql_query($sql);
while($myrow=mysql_fetch_array($query))
{
echo $myrow['id'],'+',$myrow['dateline'],'<br/>';
}
//4.mysql_close()...關閉串連
mysql_close($conn);
?>
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
///資料庫的斷開
mysql_close($db);
///資料庫的操作
//查詢
$query = "SELECT * FROM user WHERE ";
//插入
$query = "INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist');"
//刪除
$query = "DELETE FROM employees WHERE age IS NULL";
//修改
$query = "UPDATE employees SET FROM WHERE";
Townville','Telephonist');"
//結果
$result = mysql_query($query);
if(!$result)
{
echo "Error!";
exit ;
}
$num_reslutlt = mysql_num_rows($result);
//擷取當前系統時間
$AddTime = date("YmdHms");
///////////////////////////1. enroll.php 使用者註冊
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
//驗證使用者填寫資料的合理性
trim($UserMb);
trim($UserPwd);
trim($PwdConfirm);
if(!$UserMb || !$UserPwd || !$PwdConfirm)
{
echo "Your have not enter your detail message";
exit;
}
if($UserPwd != $PwdConfirm)
{
echo "Confirm Password not correct to passworld" ;
exit;
}
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
//擷取當前系統時間
$AddTime = date("YmdHms");
//插入
$query = "INSERT INTO user VALUES ('','".$UserMb."','".$UserPwd."','".$AddTime."');"
//結果
$result = mysql_query($query);
if($result)
{
///以下為成功後的相關操作
echo "Enroll Success!";
}
else
{
echo "Error!";
exit ;
}
///資料庫的斷開
mysql_close($db);
////////////////////////////////////////////使用者驗證程式碼範例
//判斷使用者名稱是否設定
if(!isset($PHP_AUTH_USER))
{
header("WWW-Authenticate:Basic realm="身分識別驗證功能"");
header("HTTP/1.0 401 Unauthorized");
echo "身分識別驗證失敗,您無權共用網路資源!";
exit();
}
/*串連資料庫*/
$db=mysql_connect("localhost","root","");
//選擇資料庫
mysql_select_db("XinXiKu",$db);
//查詢使用者是否存在
$result=mysql_query("SELECT * FROM user WHERE name='$PHP_AUTH_USER' AND password='$PHP_AUTH_PW'",$db);
if ($myrow = mysql_fetch_row($result))
{
//以下為身分識別驗證成功後的相關操作
}
else
{
//身分識別驗證不成功,提示使用者重新輸入
header("WWW-Authenticate:Basic realm="身分識別驗證功能"");
header("HTTP/1.0 401 Unauthorized");
echo "身分識別驗證失敗,您無權共用網路資源!";
exit();
}
///////////////////////2 login.php 使用者登陸
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
//驗證使用者填寫資料的合理性
trim($UserMb);
trim($UserPwd);
if(!$UserMb || !$UserPwd )
{
echo "Your have not enter your detail message";
exit;
}
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
//查詢使用者是否存在
$query = "SELECT * FROM user WHERE name='$UserMb' AND password='$UserPwd'";
$result=mysql_query($query);
if ($myrow = mysql_fetch_row($result))
{
//以下為身分識別驗證成功後的相關操作
}
else
{
//身分識別驗證不成功,提示使用者重新輸入
header("WWW-Authenticate:Basic realm="身分識別驗證功能"");
header("HTTP/1.0 401 Unauthorized");
echo "身分識別驗證失敗,您無權共用網路資源!";
exit();
}
///資料庫的斷開
mysql_close($db);
////////////////// 3. 管理員登入
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
//驗證使用者填寫資料的合理性
trim($SysUser);
trim($SysUserPwd);
if(!$SysUser || !$SysUserPwd )
{
echo "Your have not enter your detail message";
exit;
}
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
//查詢使用者是否存在
$query = "SELECT * FROM sysuser WHERE SysUser='$SysUser' AND SysUserPwd='$SysUserPwd'";
$result=mysql_query($query);
if ($myrow = mysql_fetch_row($result))
{
//以下為身分識別驗證成功後的相關操作
}
else
{
//身分識別驗證不成功,提示使用者重新輸入
header("WWW-Authenticate:Basic realm="身分識別驗證功能"");
header("HTTP/1.0 401 Unauthorized");
echo "身分識別驗證失敗,您無權共用網路資源!";
exit();
}
///資料庫的斷開
mysql_close($db);
/////////////////////// 4. 文字簡訊管理
/**********顯示簡訊********************/
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
//查詢文字簡訊
$query = "SELECT * FROM sms WHERE SmsType='$SmsType';
$result=mysql_query($query);
if ($myrow = mysql_fetch_row($result))
{
//以下為提取成功後的相關操作
}
else
{
echo "無記錄!";
exit();
}
///資料庫的斷開
mysql_close($db);
/*************添加簡訊*****************************/
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
//擷取當前系統時間
$AddTime = date("YmdHms");
//添加
$query = "INSERT INTO sms VALUES ('',$SmsType,$SmsContent,$AddTime)";
$result=mysql_query($query);
if ($myrow = mysql_fetch_row($result))
{
//以下為添加成功後的相關操作
}
else
{
echo "Add fail!";
exit();
}
///資料庫的斷開
mysql_close($db);
/*************刪除*********************************/
$Host = "localhost";
$Database = "smsdb";
$DbaUser = "root";
$DbaPassword = "";
////資料庫的串連
$db = mysql_pconnect($Host, $DbaUser,$DbaPassword);
if(!$db)
{
echo "Error: Could not connect to database.Please try again.";
exit;
}
//選擇資料庫
mysql_select_db($Database,$db);
//刪除
$query = "DELETE FROM sms WHERE SmsId = $SmsId";
$result=mysql_query($query);
if ($myrow = mysql_fetch_row($result))
{
//以下為刪除成功後的相關操作
}
else
{
echo "Delete fail!";
exit();
}
///資料庫的斷開
mysql_close($db);