PHP study Note 3 basic database operations

Source: Internet
Author: User
This article describes the most basic and practical database operations. First, let's briefly review how to use MySQL and create a table for backup. MySQL is usually installed on Linux systems. it is also very easy to install in Windows. I will not introduce it much. The following describes how to log on to mysql on Linux, create a database, and create 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 (20) primary key, Password varchar (20) not null, CreateTime timestamp default current_timestamp );
Query OK. under 0 rows affected (0.01 sec), a page is created to create a user's page. The first is a simple form:

The code is as follows:




PHP uses the $ _ POST array to obtain the data in the form submitted through the post method. In the PHP program, we first need to determine whether there is an OK field to determine whether the page is accessed for the first time or submitted after the user clicks OK, and then determine whether the two passwords are unified. The user name and password can be obtained and inserted into the database. MySQL extension or mysqli extension can be used to connect PHP to mysql databases. mysqli extension is a new one. We use this method here. Mysqli may need to be installed and configured, but it is installed by default in my environment. Using mysqli to expand the database is generally divided into the following steps: construct a mysqli object, construct a statement, bind parameters, execute, close. The code is as follows:

The code is as follows:


$ 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 ();
}
?>


The bind_param method needs to be explained slightly. the meaning of the first parameter is the parameter type. Each character corresponds to a parameter. "s" indicates a string, "I" indicates an integer, "d" indicates a floating point, and "B" indicates a blob. Finally, add a prompt message for this page:

The code is as follows:


If (! $ Match) {?>

Password and Confirm Password must match.


}
?>
If (isset ($ success )){
If ($ success ){
Echo'

User Created Successfully! ';
} Elseif ($ sucess = false ){
Echo'

User Name existed .';
}
}
?>


Next, we will compile a user list page.

The code is as follows:



















Include 'Conn. php ';$ Query = "select * from User ;";$ Res = $ mysql-> query ($ query );While ($ row = $ res-> fetch_array ()){?> }$ Res-> close ();$ Mysql-> close ();?>
User Name CreateTime Action
"> Edit
"> Delete

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.