PHP study Note 3 basic database operations

Source: Internet
Author: User

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:
Copy codeThe Code is as follows:
<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 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:
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 ();
}
?>

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:
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 will compile 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.