欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP學(xué)習(xí)筆記之三 數(shù)據(jù)庫基本操作

 更新時(shí)間:2011年01月17日 23:28:28   作者:  
本文介紹最基本最實(shí)用的數(shù)據(jù)庫操作。首先簡(jiǎn)單復(fù)習(xí)下MySQL的使用方法,并且建好一張表備用。MySQL在Linux系統(tǒng)上一般都是裝好的,在win下安裝也很簡(jiǎn)單,不多做介紹。
下面是在Linux上登錄mysql,創(chuàng)建數(shù)據(jù)庫和創(chuàng)建表的過程。

yin@yin-Ubuntu10:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 360
Server version: 5.1.41-3ubuntu12.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database UseCase;
Query OK, 1 row affected (0.00 sec)

mysql> use UseCase;
Database changed

mysql> create table User(UserName varchar(20) primary key,Password varchar(20) not null,CreateTime timestamp default current_timestamp);
Query OK, 0 rows affected (0.01 sec)下面就來建立一個(gè)頁面來完成新建用戶的頁面。首先是一個(gè)簡(jiǎn)單的表單:
復(fù)制代碼 代碼如下:

<form action="db.php" method="post">
<dl>
<dt>UserName</dt><dd><input name="UserName" maxlength="20" type="text"/></dd>
<dt>Password</dt><dd><input name="Password" maxlength="20" type="password"/></dd>
<dt>Confirm Password</dt><dd><input name="ConfirmPassword" maxlength="20" type="password"/></dd>
</dl>
<input type="submit" name="ok" value="ok"/>
</form>

PHP通過$_POST數(shù)組來獲得通過post方法提交的表單中的數(shù)據(jù)。在PHP程序中,我們首先要判斷是有OK字段,從而判斷出該頁面是首次訪問,還是用戶點(diǎn)擊OK后提交的,接著判斷兩次密碼輸入是否統(tǒng)一。然后就可以獲取到用戶名和密碼,插入數(shù)據(jù)庫中。PHP連接MySQL數(shù)據(jù)庫一般可以利用mysql擴(kuò)展或者mysqli擴(kuò)展,mysqli擴(kuò)展比較新一點(diǎn),這里我們采用這種方式。mysqli可能需要安裝配置下,不過在我的環(huán)境中是默認(rèn)裝好的。利用mysqli擴(kuò)展操作數(shù)據(jù)庫一般分為如下幾步:構(gòu)造mysqli對(duì)象,構(gòu)造statement,綁定參數(shù),執(zhí)行,關(guān)閉。代碼如下:
復(fù)制代碼 代碼如下:

<?php
$match=true;
if(isset($_POST["ok"])) {
$pwd=$_POST["Password"];
$pwdConfirm=$_POST["ConfirmPassword"];
$match=($pwd==$pwdConfirm);
$conn=new mysqli("localhost","root","123","UseCase");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query="insert into User(UserName,Password) values(?,?)";
$stmt=$conn->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('ss',$name,$pwd);
$name=$_POST["UserName"];
$pwd=$_POST["Password"];
$stmt->execute();
if($stmt->errno==0) {
$success=true;
}else {
$success=false;
}
$stmt->close();
$conn->close();
}
?>

其中bind_param方法需要稍微解釋下,第一個(gè)參數(shù)的含義是參數(shù)類型。每個(gè)字符對(duì)應(yīng)一個(gè)參數(shù),s表示字符串,i表示整數(shù),d表示浮點(diǎn)數(shù),b表示blob。最后,再為這個(gè)頁面添加一點(diǎn)提示信息:
復(fù)制代碼 代碼如下:

<?php
if(!$match) { ?>
<p>Password and Confirm Password must match.</p>
<?php
}
?>
<?php
if(isset($success)) {
if($success) {
echo '<p>User Created Successfully!';
}elseif($sucess==false) {
echo '<p>User Name existed.';
}
}
?>

再接下來,我們編寫一個(gè)用戶列表頁面。
復(fù)制代碼 代碼如下:

<table>
<tr><th>User Name</th><th>CreateTime</th><th>Action</th>
</tr>
<?php
include 'conn.php';
$query="select * from User;";
$res=$mysql->query($query);
while($row=$res->fetch_array()) {
?>
<tr>
<td><?= $row['UserName'] ?></td>
<td><?= date('Y-m-d',strtotime($row['CreateTime']))?> </td>
<td><a href="UserEdit.php?action=update&ID=<?= $row['UserName'] ?>">Edit</a>
<a href="action=delete&ID=<?= $row['UserName'] ?>">Delete</a>
</td>
</tr>
<?php
}
$res->close();
$mysql->close();
?>
</table>

相關(guān)文章

最新評(píng)論