By operation of the database mysql mysqli php

php mysqli database operations

Connect to the database

After php5.3 version, you want to connect to the database, there are two options, one through mysqlithe other outer one is through pdo. This article says that if by mysqlian object-oriented database written operation.

Before connecting to the database, you need to pass basic connection information stored in the variable good database.

$servername = "localhost"; // 地址
$username = "root"; // 用户名
$password = ""; // 密码

You can then mysqliinstantiate an object is created. And the relevant parameter.

$m = new mysqli($servername,$username,$password);

You can then judge for themselves whether an error occurred.

// 检测连接是否成功
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

Of course, the database needs to close the connection after the connection.

$conn->close();

Complete demo as follows:

<?php 
// php 连接Mysql
// 通过mysqli  

// 采用面向对象的写法来完成数据库的连接
// 设置基础数据库信息

$servername = "localhost";
$username = "root";
$password = "";

// 创建连接
$conn = new mysqli($servername,$username,$password);

// 检测连接是否成功
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

// 否则连接成功
echo "连接成功!";
// 连接之后,还需要将连接关闭
$conn->close();
 ?>

Create a database by mysqli

Create a database through mysqli, first of all we need to ensure the success of the database connection.

$servername = "localhost";
$username = "root";
$password = "";

// 创建连接
$conn = new mysqli($servername,$username,$password);    
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

Next Save the sql statement by variables.

// 设置sql语句
$sql = "create database stu_01  default character set = 'utf8' ";

After connecting the database, we need to send sql statements to create the database and verify success:

// 发送sql语句 并且验证是否创建成功
if($conn->query($sql) === TRUE){
    echo "数据库创建成功.";
}else {
    echo "数据库创建失败 ,错误信息为:" . $conn->error; // 如果失败输出错误信息
}

Of course, do not forget to finally close the database.

// 关闭数据库
$conn->close();

Note that if you use a different port (default is 3306), an empty string for the database parameters, such as: new mysqli ( "localhost", "username", "password", "", port)

Complete demo as follows:

<?php 

$servername = "localhost";
$username = "root";
$password = "";

// 创建连接
$conn = new mysqli($servername,$username,$password);    
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

// 设置sql语句
$sql = "create database stu_01  default character set = 'utf8' ";

// 发送sql语句 并且验证是否创建成功
if($conn->query($sql) === TRUE){
    echo "数据库创建成功.";
}else {
    echo "数据库创建失败 ,错误信息为:" . $conn->error; // 如果失败输出错误信息
}

// 关闭数据库
$conn->close();
?>

Create a data table by mysqi

By mysqlicreating a data table First, let's perform basic database connection, you need to pay attention to that, because we are creating a data table in the database, so the
need for a database of information about the additional configuration:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_01"; // 要操作的数据库名
// 创建连接 
$conn = new mysqli($servername,$username,$password,$dbname); // 第四个参数
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

Next you need to set a good sql statement to create a data table.

create table stu_info(
    id int(6) unsigned auto_increment primary key,
    firstname varchar(30) not null,
    lastname varchar(30) not null,
    email varchar(50),
    reg_date timestamp
    )

About sql data type, you can visit: https: //www.runoob.com/sql/sql-datatypes.html.

Some of the above sql explanation:

NOT NULL - 每一行都必须含有值(不能为空),null 值是不允许的。
DEFAULT value - 设置默认值
UNSIGNED - 使用无符号数值类型,0 及正数
AUTO INCREMENT - 设置 MySQL 字段的值在新增记录时每次自动增长 1
PRIMARY KEY - 设置数据表中每条记录的唯一标识。 通常列的 PRIMARY KEY 设置为 ID 数值,与 AUTO_INCREMENT 一起使用。

Each table should have a primary key (present as "id" column), the primary keys must contain unique values.

In order to perform sql, sql statement above may be stored in a variable which:


// 使用sql 创建数据表
$sql = "create table stu_info(
    id int(6) unsigned auto_increment primary key,
    firstname varchar(30) not null,
    lastname varchar(30) not null,
    email varchar(50),
    reg_date timestamp
)";

Disposed over sql statement, sql statement can be sent directly query method, and determines whether they were successful.

if($conn->query($sql) === TRUE){
    echo "数据表创建成功。";
}else {
    echo "数据表创建失败,错误信息:" . $conn->error;
}

// 关闭数据库连接
$conn->close();

Complete demo as follows:

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_01";
// 创建连接
$conn = new mysqli($servername,$username,$password,$dbname);    
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}



// 使用sql 创建数据表
$sql = "create table stu_info(
    id int(6) UNSIGNED auto_increment primary key,
    firstname varchar(30) not null,
    lastname varchar(30) not null,
    email varchar(50),
    reg_date timestamp
)";

if($conn->query($sql) === TRUE){
    echo "数据表创建成功。";
}else {
    echo "数据表创建失败,错误信息:" . $conn->error;
}

// 关闭数据库连接
$conn->close();

?>

 Insert information into the data table by mysqli

First, connect to the database and the database to determine the correct connection.

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_01";
// 创建连接
$conn = new mysqli($servername,$username,$password,$dbname);    
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

Next, set sql statement:

// 设置插入数据的sql语句 
$sql = "insert into stu_info(firstname,lastname,email) values('张三','张小三','[email protected]')";

Will set a good sql statement sent:

// 发送sql 语句 
if($conn->query($sql) === TRUE){
    echo "新记录添加成功!";
}else {
    echo "新记录添加失败,错误信息:" . $conn->error;
}

// 关闭连接
$conn->close();

Complete demo as follows:

<?php 
header("Content-type:text/html;Charset=utf-8");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_02";
// 创建连接
$conn = new mysqli($servername,$username,$password,$dbname);    
if($conn->connect_error){
    die("连接失败,错误:" . $conn->connect_error);
}

// 设置插入数据的sql语句 
$sql = "insert into stu_info(firstname,lastname,email) values('张三','张小三','[email protected]')";


// 发送sql 语句 
if($conn->query($sql) === TRUE){
    echo "新记录添加成功!";
}else {
    echo "新记录添加失败,错误信息:" . $conn->error;
}

// 关闭连接
$conn->close();
?>

If the stored data when the garbled, you can use the following code to solve:

mysqli_set_charset($conn,'utf8'); // 解决乱码问题

Complete demo as follows:

<?php

header("Content-type:text/html;Charset=utf8");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_02";

$conn = new mysqli($servername,$username,$password,$dbname);

mysqli_set_charset($conn,'utf8'); // 解决乱码问题

if($conn->connect_error){

    echo "连接失败,错误信息:" . $conn->connect_error;
}

// 设置sql语句
$sql = "insert into stu_info(firstname,lastname,email) values('李四','李小思','[email protected]')";

// 发送sql
if($conn->query($sql) === TRUE) {
    echo "新纪录添加成功.";
}else {
    echo "新纪录添加失败,错误信息:" . $conn->connect_error;
}

// 关闭
$conn->close();

Above we inserted a message to the data in the table, let's try a disposable insert multiple pieces of information into the database:

Complete demo as follows:

<?php

// 数据库连接基本信息
header("Content-type:text/html;Charset=utf8");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_02";

$conn = new mysqli($servername,$username,$password,$dbname);

mysqli_set_charset($conn,'utf8'); // 解决乱码问题
// 检查链接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
// 下面开始插入多条数据  

$sql = "insert into stu_info(firstname,lastname,email) values('张三丰','张君宝','[email protected]');";
// 使用.= 的形式进行sql语句连接
$sql .= "insert into stu_info(firstname,lastname,email) values('东方不败','东方拜拜','[email protected]');";

// 发送sql
if($conn->multi_query($sql) === TRUE) {
    echo "数据添加成功!";
}else{
    echo "数据添加失败,错误信息如下: " . $conn->connect_error;
}

// 关闭
$conn->close();

In the above code we use .=for splicing sql statement form, a disposable insert in order to achieve a plurality of data.

Note that each SQL statement must be separated by a semicolon.
In the above code, we replaced the query method multi_query () method can be implemented smoothly inserted into a plurality of data-time.

Read data mysqli

The above data we inserted through mysqli, here we come to learn how to read out the data:

If you need to query all the data:

<?php

// 数据库连接基本信息
header("Content-type:text/html;Charset=utf8");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_02";

$conn = new mysqli($servername,$username,$password,$dbname);

mysqli_set_charset($conn,'utf8'); // 解决乱码问题
// 检查链接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 


// 设置sql 语句,查询全部数据 
$sql = "select * from stu_info";

// 发送sql语句,获得查询结果
$result = $conn->query($sql);

// 如果查询的结果>0表示查询成功,那么就可以将数据输出 
// 函数 num_rows() 判断返回的数据。

// 如果返回的是多条数据,函数 fetch_assoc() 将结合集放入到关联数组并循环输出。
if($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()){
        echo "id" . $row['id'] . '- Name:' . $row['firstname'] . " " . $row['lastname'] . '<br>';
    }
}else {
    echo "暂无数据";
}

// 关闭
$conn->close();

In the above demo, we use the sqlstatement as follows:

select * from stu_info;

The above statement can help us to query all the data, we just want to check if a part of the data or you can change it:

select id ,firstname,lastname from stu_info;

 Applications where the statement

Where we can add conditions on the basis of the original conditions on the query data, let's find more precise, for example:

select * from stu_info where id > 3;

 Delete data through mysqli

Let's look through mysqli delete data in the database.

<?php


// 数据库连接基本信息
header("Content-type:text/html;Charset=utf8");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stu_02";

$conn = new mysqli($servername,$username,$password,$dbname);

mysqli_set_charset($conn,'utf8'); // 解决乱码问题
// 检查链接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 


// 设置sql
$sql = "delete from stu_info where lastname = '李小思';";

// 发送sql 
if($conn->query($sql) === TRUE) {
    echo "删除成功";
}else {
    echo "删除失败,错误信息为:" . $conn->connect_error;
}

$conn->close();

When you delete data, be sure to delete from stu_infoadd the condition where the back, otherwise the entire table will be empty.

Guess you like

Origin www.cnblogs.com/liujunhang/p/10926632.html