"PHP" MySQL basic operation integration

Source: Internet
Author: User

PHP Versions: PHP version 5.5.1

Environment: WINDOWS10 Xmapp

Database: MySQL

Code:

<?PHPfunctionCONNETIONDB ($servername,$username,$password,$db) {   //Create a connection    $conn=Mysqli_connect($servername,$username,$password,$db); //Detecting Connections    if(!$conn) {         die("Connection failed:".)Mysqli_connect_error()); return FALSE; }    return $conn;}//Create a databasefunctionCREATE_DB ($dbname,$conn) {     $sql= "CREATE DATABASE"$dbname‘"; if(Mysqli_query($conn,$sql)) {         Echo"DB created successfully"; }Else {         Echo"Error:".Mysqli_error($conn); }}//New TablefunctionCreate_table ($conn) {    $sql= "CREATE TABLE account (ID INT (6) UNSIGNED auto_increment PRIMARY KEY, username VARCHAR (+) not NULL, PASSW Ord VARCHAR (+) not NULL)"; if(Mysqli_query($conn,$sql)) {        Echo"Table created successfully"; }Else{        Echo"Error:".Mysqli_error($conn); }}//Inserting DatafunctionInsert$username,$password,$conn){    $sql= "INSERT into account (username, password) VALUES ('$username‘, ‘$password‘)"; if(Mysqli_query($conn,$sql)) {        Echo"Insert successfully<br>"; }Else{        Echo"Error:".Mysqli_error($conn); }}//Querying DatafunctionGet$username,$conn) {    $sql= "Select password from account WHERE username = '$username‘"; $result=Mysqli_query($conn,$sql); if(!$result) {        printf("Error:%s\n",Mysqli_error($conn)); Exit(); }     while($row=Mysqli_fetch_array($result)) {//echo "Username:". $username. "Password:". $row [' Password '];//echo ' <br> ';        return $row[' Password ']; }}//Delete DatafunctionDelete$username,$password,$conn) {    $sql= "DELETE from account WHERE username = '$username' and password = '$password‘"; if(Mysqli_query($conn,$sql)) {        EchoThe Delete$usernameSuccessfully<br> "; }Else{        Echo"Error:".Mysqli_error($conn); }}//Update DatafunctionUpdate$username,$password,$new _username,$new _password,$conn) {    $sql= ' '; if($new _username= = "and$new _password! = ") {        $sql= "UPDATE account SET Password = '$new _password' WHERE username = '$username' and password = '$password‘"; }Else if($new _username! = "and$new _password= = "'){        $sql= "UPDATE account SET username = '$new _username' WHERE username = '$username' and password = '$password‘"; }Else if($new _username! = "and$new _password! = "){        $sql= "UPDATE account SET username = '$new _username', password = '$new _password' WHERE username = '$username' and password = '$password‘"; }    if(Mysqli_query($conn,$sql)) {//echo "Update successfully";        return true; }Else {//echo "Error:". Mysqli_error ($conn);        return false; }}//Close ConnectionfunctionClosedb ($conn) {    Mysqli_close($conn);}

Use: direct include "db.php" can call the method in this.

"PHP" MySQL basic operation integration

Related Article

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.