1,連接數據庫
<?PHP
$dbhost = 'localhost';
$dbuser = 'root'; //你的MySQL用戶名
$dbpass = '123456'; //你的MySQL密碼
$dbname = 'data'; //你的MySQL庫名
//連接本地數據庫
$GLOBALS["conn"] = MySQL_connect($dbhost,$dbuser,$dbpass);
//打開數據庫
MySQL_select_db($dbname,$GLOBALS["conn"]);
?>
2.讀取數據庫中,某一字段值
<?PHP
//讀取一列數據
$sql="select * from ec_admin";
$result = MySQL_query($sql,$GLOBALS["conn"]);
printf("用戶名: %s<br>\n", MySQL_result($result,3,"UserName"));
printf("密碼: %s<br>\n", MySQL_result($result,3,"UserPass"));
?>
3,插入某一條數據
<?PHP
$sql="insert into ec_admin(UserName,UserPass) values('liugongxun2','jakemary2')";
$result=mysql_query($sql,$GLOBALS["conn"])or dIE(MySQL_error());
?>
4,while循環
<?PHP
$sql="select * from ec_admin";
$result = MySQL_query($sql,$GLOBALS["conn"]);
while($myrow = MySQL_fetch_row($result))
{
printf("用戶名%s %s密碼<br />",$myrow[1],$myrow[2]);
}
?>
5,do while循環
<?PHP
$sql="select * from ec_admin";
$result = MySQL_query($sql,$GLOBALS["conn"]);
if($myrow = MySQL_fetch_array($result))
{
do
{
printf("用戶名%s %s密碼<br />",$myrow["UserName"],$myrow["UserPass"]);
}while($myrow = MySQL_fetch_array($result));
}
?>
6,判斷表單是否提交
<?PHP
if ($submit)
{}
?>