PHP Connection Database

Source: Internet
Author: User

$mysql _server_name= ' localhost ';//change to your MySQL database server $mysql _username= ' root ';//change to your MySQL database username MySQL default is the root user name, you can also change $mysql _password= ' root ';//change to your own database password$mysql _database= ' Cai ';//change to your MySQL database name$con=Mysqli_connect($mysql _server_name,$mysql _username,$mysql _password,$mysql _database);if(!$con) {     die("Database Server Connection Failed") ; } Else{     Echo' Database connection succeeded '; }

This makes it possible to connect to the database.

Here's how to create a database:

$sql = "CREATE DATABASE caicai"; SQL statement Database create CAICAI database name
$conn->query ($sql)//Run the SQL statement. Mysqli_query ($conn, $sql) This statement can also be created successfully.

The previous object-oriented notation, followed by another notation.

  $sql  = "create TABLE myguests (/*sql statement can be navical for MySQL software when creating tables view */id Int (6) UNSIGNED auto_increment PRIMARY KEY,/* Field ID data type INT auto_increment auto Increment unsig NED unsigned PRIMARY key primary key */firstname varchar ($) Not null,/* field FirstName data type varchar character type */lastname varchar (+) not NULL , email VARCHAR (reg_date TIMESTAMP)  ";  if  ( $conn ->query ( $sql ) = = = true  ) { echo " Create succeeded " ;}  else   {  echo  "Create data Table Error:".  $conn ->error;}  

Note in the SQL statement I cannot use//to comment on this side.

Inserting data

$firstname = ' John '; $lastname = ' Doe '; $email = ' [email protected] '; $sql = "INSERT into Myguests (firstname, LastName, email) VALUES ('$firstname', '$ LastName', '$email') ";

It's strange to use a variable with quotes.

Insert multiple data at a time

$sql = "INSERT into Myguests (firstname, LastName, email) VALUES (' John ', ' Doe ', ' [email protected] '), (' Mary ' , ' Moe ', ' [email protected] ', (' Julie ', ' Dooley ', ' [email protected] ')";

Reading data

$sql= "SELECT ID, firstname, LastName, email from myguests";$result=$conn->query ($sql); if($result->num_rows > 0) {    //Output Data     while($row=$result-Fetch_assoc ()) {        Echo"ID:".$row["id"]. "-Name:".$row["FirstName"]. " " .$row["LastName"]. "".$row["Email"]. " <br> "; }} Else {    Echo"0 Results";}

When Echo uses dots to connect fields and variables

Where:

$result Mysqli_query ($conn, "select * from myguests WHERE firstname= ' Mary '"); // The where filter  finds the data firstname= ' Mary ' in the Myguests table  .  if($rowmysqli_fetch_array($result)) {        Echo  $row$row[' LastName ']. " " . $row [' email '];     Echo "<br>";}

Sort ORDER BY:

$result Mysqli_query ($conn, "select * from Myguests ORDER by Reg_date");  while ($rowmysqli_fetch_array($result)) {    echo$row[' FirstName '];     Echo "  $row[' LastName '];     Echo "<br>";}

Updata:

Mysqli_query ($conn, "UPDATE myguests SET id=110where firstname= ' Julie ' and lastname= ' Dooley '");

Delete

Mysqli_query ($conn, "DELETE from myguests WHERE lastname= ' Moe '");

PHP Connection 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.