PHP Learning notes of the three database basic operations

Source: Internet
Author: User
Here is the process of logging on to MySQL on Linux, creating a database and creating a table.

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 () primary key,password varchar () not null,createtime timestamp default CU Rrent_timestamp);
Query OK, 0 rows affected (0.01 sec) below to create a page to complete the new user's page. The first is a simple form:
Copy CodeThe code is as follows:
<form action= "db.php" method= "POST" >
<dl>
<dt>username</dt><dd><input name= "UserName" maxlength= "type=" "Text"/></dd>
<dt>password</dt><dd><input name= "Password" maxlength= "type=" Password
<dt>confirm password</dt><dd><input name= "ConfirmPassword" maxlength= "type="/ ></dd>
</dl>
<input type= "Submit" Name= "OK" value= "OK"/>
</form>

PHP uses the $_post array to obtain data from the form submitted by the Post method. In the PHP program, we first have to determine whether there are OK fields, so that the page is the first visit, or the user click OK after the submission, and then judge two times password input is unified. The user name and password can then be retrieved and inserted into the database. PHP connection MySQL database can generally take advantage of MySQL expansion or mysqli extension, mysqli extension is newer, here we use this approach. Mysqli may need to be installed in the configuration, but it is installed by default in my environment. Using the Mysqli extended operation database is generally divided into the following steps: Constructing MYSQLI objects, constructing statement, binding parameters, executing, closing. The code is as follows:
Copy CodeThe code is as follows:
<?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 ();
}
?>

Where the Bind_param method requires a little explanation, the first parameter means a parameter type. Each character corresponds to a parameter, s represents a string, I represents an integer, d represents a floating-point number, and B represents a blob. Finally, add a little hint to this page:
Copy CodeThe code is as follows:
<?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. ';
}
}
?>

Next, we'll write a user list page.
Copy CodeThe code is as follows:
<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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.