PHP Full Stack Development (vii): PHP interacts with MySQL storage (1. Connect, create a database, create a data table)

Source: Internet
Author: User
Tags mysql command line

Commands can be used in Linux servers

mysqladmin-u root password beijing2007;

To change the password for the root user of MySQL.

But in our own installation of Wampserver computer, you can go to phpmyadmin this page page to change, the initial root password is also empty.

Then we use the process-oriented mysqli to link the database, the link code is as follows:

<?PHP$servername= "localhost";$username= "Root";$password= "Beijing2007"; //Create a connection$conn=Mysqli_connect($servername,$username,$password); //Detecting Connectionsif(!$conn) {     die("Connection failed:".)Mysqli_connect_error());}Echo"Connection succeeded";?>

The link closes automatically after the script has finished executing, or it can be closed manually using the following code:

Mysqli_close ($conn);

Okok, now that we have a link, now let's create a database

<?PHP$servername= "localhost";$username= "Root";$password= "Beijing2007"; //Create a connection$conn=Mysqli_connect($servername,$username,$password); //Detecting Connectionsif(!$conn) {     die("Connection failed:".)Mysqli_connect_error());}Echo"Connection succeeded";$sql= "CREATE DATABASE mydb";if(Mysqli_query($conn,$sql)){    Echo"CREATE Database Succeeded";}Else{    Echo"Error Creating Database:".Mysqli_error($conn);}?>

The above code we created a database called MyDB.

After creating the database, we will create a data table in the database

Then the data table is used to store the user information, then it has the following fields: Account number, password, mobile phone numbers.

Then we need to consider the types of these fields when creating the data tables.

This time we will learn the data types in MySQL, this is quite difficult.

In MySQL, there are three main types, the text, the number, and the date/time (Date/time)

Here the user name we use the type is varchar type; password we use the varchar type; the cell phone number uses the int type.

VARCHAR (size): holds a variable-length string that can contain letters, numbers, special characters, the maximum length of a string specified in parentheses, a maximum of 255 characters, or a text type if the value is longer than 255.

INT (size): Signed range-2147483648 to 2147483647, unsigned range 0 to 4294967295. Size defaults to 11

Now that the preparatory work is done, let's create a data sheet:

<?PHP$servername= "localhost";$username= "Root";$password= "Beijing2007";$dbname= "MyDB"; //Create a connection$conn=Mysqli_connect($servername,$username,$password,$dbname); //Detecting Connectionsif(!$conn) {     die("Connection failed:".)Mysqli_connect_error());}Echo"Connection succeeded";//Create a data table using SQL$sql="CREATE TABLE User (ID INT (6) UNSIGNED auto_increment PRIMARY key,user varchar (ten) not null,pwd VARCHAR (Ten) not null,t El int,reg_date TIMESTAMP)";if(Mysqli_query($conn,$sql)){    Echo"Create data Table succeeded";}Else{    Echo"Error creating:".Mysqli_error($conn);}//To close a database connectionMysqli_close($conn);?>

When we created the data table, we noticed that there was one more parameter to the database in the Mysqli_connect function $dbname

Because when we create the data table, we have to create the database.

And after that, we open the MySQL command line and verify our operation.

Mysql> show databases; +--------------------+| Database           |+--------------------+| information_schema | | mydb               | | mysql              | | performance_schema | | SYS                |+ --------------------+5inset (0.00 sec)

You can see the database we created MyDB

mysql> use mydb;database changedmysql> show tables; +----------------+| Tables_in_mydb |+----------------+| User           |+----------------+1inset (0.00 sec)

You can see that the database mydb has our data sheet user

Then we'll look at the fields in our user table:

Mysql> Show Columns fromuser;+----------+-----------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+-----------------+------+-----+---------+----------------+| ID |int(6) unsigned | NO | PRI | NULL | auto_increment | | user | varcharTen)     |     NO | |                NULL | || PWD | varcharTen)     |     NO | |                NULL | || Tel |int( One)         |     YES | |                NULL | || Reg_date | Timestamp |     YES | |                NULL | |+----------+-----------------+------+-----+---------+----------------+5Rowsinch Set(0.01Sec

You can see the fields.

It's getting harder to feel powerless!

Feel the Gas

PHP Full Stack Development (vii): PHP interacts with MySQL storage (1. Connect, create a database, create a data table)

Related Article

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