1,php連接數據庫
<?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讀取數據庫中,某一字段值
<?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插入某一條數據
<?php
$sql="insert into ec_admin(UserName,UserPass) values('liugongxun2','jakemary2')";
$result=mysql_query($sql,$GLOBALS["conn"])or die(mysql_error());
?>
4,php 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,php 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判斷表單是否提交
<?php
if ($submit)
{}
?>