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)
{}
?>