PHP connection MySQL database and delete and change

Source: Internet
Author: User

1. Connect MySQL database $conn = new Mysqli ($host, $user, $password);

 $conn, query (' Set names UTF8 '); //Set character encoding to avoid storing Chinese data garbled
$conn-select_db (' db ');
//Select the database (db) you have set up

1.1 determine connection status
If (! $conn) {
Die ("
Database connection Exception ");
}

//Database creation
$db = "CREATE Database db";
if ($conn, query ($db)) {
echo "successfully created database ";
}else{
echo "Create failed ";
}

//data table creation
$table = "CREATE TABLE table_name (
ID int (TEN) unsigned auto_increment unique primary key,
user varchar (+) not NULL
);";

1.1.1 Adding Data Method 1: Preprocessing statements and binding parameter methods (for execution of multiple query statements)
$table = $conn Prepare ("INSERT into user_table (Id,user) VALUES (NULL,?)"); / Prepare SQL statements
$name = ' Xiao Ming ';
$table-Bind_param ("s", $name); (Type: s, type value), parameter S is string,i to interger,d and double, type value can only be a variable
if ($table, execute () && $conn-affected_rows) { //Decide whether to add success
echo "Add success";
}else{

}
1.1.1 Add Data Method Two: Directly call the query () method (suitable for a single statement; multiple ";" Separated, using Multi_query () method to query);
$insert = "INSERT into table (name) VALUES (' Hello ')";
if ($conn, query ($insert) && $conn, affected_rows) {
echo "added successfully";
}else{
echo "Add failed";
}

1.1.2 Delete Data Method 1 call the query () method directly;
$delete = "Delete from user_table where user in (' Zhang San ', ' 1 ') or ID in (1)";//Delete column name of User= ' Zhang San ' or id=1 column name
if ($conn, query ($delete) && $row = $conn affected_rows) {
"Delete succeeded, altogether: $row". Line ";
}else{
Ehco "Delete Failed";
}
 //1.1.2 Deleting Data Method 2: preprocessing statements and binding parameters
$delete = $conn Prepare ("Delete from user_table where ID in (?) or user in (?); ");
$id = 1;
$name = ' Zhang San ';
$delete-Bind_param (' II ', $id, $name);
if ($delete, execute () && $row = $conn-affected_rows) {
echo "Delete succeeded, altogether: $row". Line ";
}else{
echo "Delete Failed";
}

1.1.3 Change Data Method 1: Direct query () method
$update = "Update user_table set user = ' Miss Liu ' where id = ' 115 ';";
if ($conn, query ($update) && $row = $conn affected_rows) {
echo "Change: $row". " Line ";
}else{
echo "Failed to change";
}

//1.1.3 Change Data Method 2: preprocessing statements and binding parameters
$update = $conn-Prepare ("update user_table set user =?") where id =?; ");
$user = "Miss Huang";
$id = 1;
$update-Bind_param ("Si", $user, $id);
if ($update, execute () && $row = $conn-affected_rows) {
echo "Change: $row". " Line ";
}else{
echo "Failed to change";
}
1.1.4 Query data Method 1: Direct query () method
$select = "Select Id,user from User_table ORDER by User,id DESC";$result = Query ($select), $conn
if ($result-num_rows>0) {//ORDER BY sort desc descending sort
while ($row = $result, Fetch_array ()) {//Loop query and return each statement of the specified condition
echo "$row [id]: $row [user]<br>";
}
}else{
echo " no data ";
}
1.2 Process-oriented approach
$conn = Mysqli_connect ($this->host, $this, user, $this-password);
Mysqli_query ($conn, ' Set names UTF8 '); Set character encoding to avoid storing Chinese data garbled
mysqli_select_db ($conn, ' db '); //Select the database (db) you have set up

1.2.1 Additions and deletions (similarly above);
That is:mysqli_query () = = $conn query ();

1.3 PDO method (slightly)


PHP connection MySQL database and delete and change

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.