Php:mysqli "process-oriented" operational database "Connect, build, build, add, delete, change, check, close"

Source: Internet
Author: User
Tags db2

<?php/** * Database operation key function *mysql_connect: Connection data *mysql_error: The last SQL action error message *mysqli_query: Execute SQL statement, delete the check *mysql_select_    DB: Select Database *mysql_fetch_array: Fetch 1 Query records from query results *mysql_close: Close database connection */function println ($msg) {echo "<br>"; echo $msg;} /** Database Configuration */$mysql _server_name = "localhost"; Change to your MySQL database server $mysql_username = "root"; Change to your MySQL database user name $mysql_password = ""; Change to your MySQL database password $mysql_database = "DB2"; Change to your MySQL database name $mysql_table = "Person"; Change to your table name/** * Connect to Database */$con = Mysqli_connect ($mysql _server_name, $mysql _username, $mysql _password); Connect to the database if (! $con) {die (' Could not connect: '. Mysqli_error ($con));} /** * Delete Database: DB2 */$sql _delete_db = "DROP database $mysql _database", if (Mysqli_query ($con, $sql _delete_db)) {println ("$ sql_delete_db OK ");} else {println ("$sql _delete_db failed:". Mysqli_error ($con));} /** * CREATE DATABASE: DB2 */$sql _create_db = "CREATE Database $mysql _database", if (Mysqli_query ($con, $sql _create_db)) {println ( "Create OK");} else {println ("Create failed:". Mysqli_error ($con));} /** * Select database; DB2 */mysqli_select_db ($con, $mysql _database);/** * create data table; Person */$sql _create_table = "CREATE TABLE $mysql _t Able (id int not NULL auto_increment,primary KEY (ID), name varchar (all), age int) "; if (Mysqli_query ($con, $sql _create_table ) {println ("CREATE table OK");} else {println ("CREATE TABLE failed:". Mysqli_error ($con));}    /** * Delete data from table (person), */$sql _delete = "Delete from $mysql _table where age = $"; if (Mysqli_query ($con, $sql _delete)) { println ("Delete table OK");} else {println ("Delete table failed:". Mysqli_error ($con));} /** * Insert new data into the table (person), */$age = rand (12, 80);//randomly spawn age $sql_inset = "INSERT INTO $mysql _table (name,age) value (' Flying_$ag  E ', $age) ", if (Mysqli_query ($con, $sql _inset)) {println (" Insert Table OK "),} else {println (" Insert Table failed: ". Mysqli_error ($con));} /** * Query data from table (person); */$sql _select = "SELECT * from $mysql _table order BY"; $result = Mysqli_query ($con, $sql _select );/** output query result */while ($row = Mysqli_fetch_array ($result)) {println ($row [' ID ']. " " . $row [' name ']. " " . $row [' Age '];} $result->close ();/** * Update table (person) data; */$sql _update = "Update $mysql _table Set age = + where Age <"; $result = Mysqli_query ($con, $sql _update);p rintln ($result), if ($result) {println ("sql_update table OK"),} else {println ("sql _update table failed: ". Mysqli_error ($con));} /** * Close Database connection */mysqli_close ($con);

  

Php:mysqli "process-oriented" operational database "Connect, build, build, add, delete, change, check, close"

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.