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