PHP Simple connection operation to MySQL database

Source: Internet
Author: User

Some of the former have been busy with the company's front-end static page of things, after a simple study of the basic knowledge of PHP, today I want to review the way PHP connect to the database, write the essay to save a look

PHP connects to the database port and creates a new database

<?PHP$servername= "localhost"; $username= "Root"; //Create a connection        $conn=Mysqli_connect($servername,$username); //Detecting Connections        if(!$conn) {             die("Connection failed:".)Mysqli_connect_error()); }        //Create a database        $sql= "CREATE DATABASE MyDB"; if(Mysqli_query($conn,$sql)) {            Echo"Database Creation succeeded"; } Else {            //database already exists            Echo"Error Creating Database:".Mysqli_error($conn); }        Mysqli_close($conn); ?>

After a successful connection, create a simple data table

//Create a data table using SQL        $sql= "CREATE TABLE myguests (ID INT (6) UNSIGNED auto_increment PRIMARY KEY, FirstName VARCHAR () not NULL, LastName varchar (+) not NULL, email VARCHAR (+), reg_date TIMESTAMP)"; if($conn->query ($sql) ===TRUE) {            Echo"Table Myguests created successfully"; } Else {            Echo"Create data Table Error:".$conn-error; }

When connecting to the database, to ensure that the database server port is open, otherwise cannot connect to, before writing code, you can use NAVICAT to test the database connection

When learning, open the database service, can be through the WAMP integration environment to do

PHP Simple connection operation to MySQL database

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.