PHP Database Operations-create libraries and tables and insert data __ Database

Source: Internet
Author: User
Tags php database

Above we correctly connect to the MySQL database, this article will further create a database, table, fill in the table data.

You know, connect to the database to do the operation, the same code moved over

<?php
*
 * Database operations * (CREATE DATABASE, table, insert data, insert multiple data) * To change the template for this
 generated file go to
 * W Indow-preferences-phpeclipse-php-code Templates
 */
//First connect to database
$servername = "localhost";
$username = "root";
$userpassword = "********";

$connent =new mysqli ($servername, $username, $userpassword);
if ($connent->connect_error) {
	die ("Connection failed:".) $connent->connect_error);
} else{
	echo "connected successfully";
}
Create DATABASE
$createdatabase = "Create db TestDB";
if ($connent->query ($createdatabase) ==true) {
	echo "CREATE Database succeeded";
} else{
	echo "Error Creating Database:". $connent->error;
}
? >

If the creation succeeds--"CREATE DATABASE Success Error creating Database:can ' t creating db ' TestDB '; The database exists has been proven to exist.

In addition, we can open navicat for MySQL to view


Next, follow the build table:

CREATE TABLE  native Build statement ID self-add unique name Age  email
$createtable = "CREATE TABLE en" (id int) UNSIGNED auto_increment PRIMARY key,name text,age Int (3), email text) ";
if ($connent->query ($createtable) ==true) {//execute
	echo "CREATE table en success";
} else{
	echo "Error Creating table:". $connent->error;
}
Output Creation Table succeeded

Access to Software view:


Then insert the data, and here are the native statements that are used.

Insert data
$insertdata = "insert into en (name,age,email) VALUES (' Zhanghao ', N, ' 142*******112@110.com ')";
if ($connent->query ($insertdata) ==true) {
	echo succeeded in inserting data;
} else{
	echo "Error Insert data:". $connent->error;
}
It can also be as simple as this
. Some/*if (Mysqli_query ($connent, $insertdata)) {
    echo "successfully inserted data";
} else {
    echo " Error Insert Data: ". $connent->error;
} *

///Close Database
mysqli_close ($connent);


Finally, to close the database, or you will always add the next many I'll annotate him first.

Insert more than one data
$insertdatas = "insert into en (name,age,email) VALUES (' Test1 ', 1, ' 1.com ');";
$insertdatas. = "insert into en (name,age,email) VALUES (' Tes2 ', 2, ' 2.com ');";
$insertdatas. = "insert into en (name,age,email) VALUES (' Test3 ', 3, ' 3.com ')";
if ($connent->multi_query ($insertdatas) ==true) {
    echo "Inserts multiple data successfully";
} else {
    echo "Error insert Datas:". $connent->error;
}
Closes the database
mysqli_close ($connent);



Complete code Slice---

<?php * * Database operations * (CREATE DATABASE, table, insert data, insert multiple data) * To change the template for this generated file go to * window-preferen
Ces-phpeclipse-php-code Templates *///First connect to database $servername = "localhost";
$username = "root";
$userpassword = "********";

$dbname = "TestDB";
$connent =new mysqli ($servername, $username, $userpassword, $dbname); if ($connent->connect_error) {die ("Connection failed:".)
$connent->connect_error);
}else{echo "connected successfully";//CREATE DATABASE $createdatabase = "Create db TestDB"; if ($connent->query ($createdatabase) ==true) {echo "CREATE database succeeded";} else{echo "Error Creating Database:". $connent->error. "
<br> "; }//CREATE TABLE native build statement ID self-add unique name age Email $createtable = "CREATE TABLE en" (id int (a) UNSIGNED auto_increment PRIMARY key,name
Text,age Int (3), email text) "; if ($connent->query ($createtable) ==true) {//execute echo "CREATE table en success";} else{echo "Error Creating table:". $connent->error. "
<br> "; }//Insert data/* $insertdata = INSERT into en (name,age,email) VALUES (' Zhanghao ', 23, ' 142*******112@110.com ') "; if ($connent->query ($insertdata) ==true) {echo succeeded in inserting data;} else{echo "Error Insert data:".
$connent->error; }*///can also be written as follows this is also relatively simple some/*if (Mysqli_query ($connent, $insertdata)) {echo "successfully inserted data";} else {echo "Error insert Dat A: ".
$connent->error;
}*///Insert multiple data/* $insertdatas = INSERT into en (name,age,email) VALUES (' Test1 ', 1, ' 1.com '); ";
$insertdatas. = "insert into en (name,age,email) VALUES (' Tes2 ', 2, ' 2.com ');";
$insertdatas. = "insert into en (name,age,email) VALUES (' Test3 ', 3, ' 3.com ')"; if ($connent->multi_query ($insertdatas) ==true) {echo "Inserts multiple data successfully";} else {echo "Error insert Datas:".
$connent->error; }*///Close Database Mysqli_close ($connent);?>
The following will be modified, deleted, conditional query, etc.-------

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.