Home >Backend Development >PHP Tutorial >PHP Study Notes 3 Basic Database Operations_PHP Tutorial

PHP Study Notes 3 Basic Database Operations_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:32:16862browse

The following is the process of logging in 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; varchar(20) primary key,Password varchar(20) not null,CreateTime timestamp default current_timestamp);
Query OK, 0 rows affected (0.01 sec) Let’s create a page to create a new user page. First is a simple form:



Copy code

The code is as follows:
UserName

Password

Confirm Password






PHP obtains submission through the post method through the $_POST array data in the form. 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 clicked OK, and then determine whether the two password inputs are consistent. Then you can get the username and password and insert them into the database. PHP can generally use mysql extension or mysqli extension to connect to the MySQL database. The mysqli extension is relatively new, so here we use this method. Mysqli may need to be installed and configured, but it is installed by default in my environment. Using mysqli extension to operate the database is generally divided into the following steps: constructing the mysqli object, constructing the statement, binding parameters, executing, and closing. The code is as follows:


Copy code
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: %sn", 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 a little explanation. The meaning of the first parameter is the 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 prompt information to this page:


Copy the code
The code is as follows: if(!$match) { ?>

Password and Confirm Password must match.


}
?>
< ;?php
if(isset($success)) {
if($success) {
echo '

User Created Successfully!';
}elseif($sucess==false ) {
echo '

User Name existed.';
}
}
?>


Next, we write a user list page .



Copy code
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 NameCreateTimeAction
Edit
Delete

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/322815.htmlTechArticle下面是在Linux上登录mysql,创建数据库和创建表的过程。 yin@yin-Ubuntu10:~$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn