PHP學習筆記之三 資料庫基本操作_php技巧

來源:互聯網
上載者:User
下面是在Linux上登入mysql,建立資料庫和建立表的過程。

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)下面就來建立一個頁面來完成建立使用者的頁面。首先是一個簡單的表單:
複製代碼 代碼如下:

<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數組來獲得通過post方法提交的表單中的資料。在PHP程式中,我們首先要判斷是有OK欄位,從而判斷出該頁面是首次訪問,還是使用者點擊OK後提交的,接著判斷兩次密碼輸入是否統一。然後就可以擷取到使用者名稱和密碼,插入資料庫中。PHP串連MySQL資料庫一般可以利用mysql擴充或者mysqli擴充,mysqli擴充比較新一點,這裡我們採用這種方式。mysqli可能需要安裝配置下,不過在我的環境中是預設裝好的。利用mysqli擴充操作資料庫一般分為如下幾步:構造mysqli對象,構造statement,綁定參數,執行,關閉。代碼如下:
複製代碼 代碼如下:

<?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方法需要稍微解釋下,第一個參數的含義是參數類型。每個字元對應一個參數,s表示字串,i表示整數,d表示浮點數,b表示blob。最後,再為這個頁面添加一點提示資訊:
複製代碼 代碼如下:

<?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.';
}
}
?>

再接下來,我們編寫一個使用者列表頁面。
複製代碼 代碼如下:

<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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.