PHP connection MySQL Database

Source: Internet
Author: User

To connect to a database:use an object-oriented approach;1. Create a Mysqli object, create a Connection object2. Prepare an SQL statement3. Execute the SQL statement, if it is a query statement. Successful return of results and objects4. Find the data from the result set object;Query Property Code:
<?PHP//Create a Mysqli object, create a Connection object$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//The parentheses fill in the IP address domain name, username, password, database name//prepare an SQL statement$sql= "SELECT * FROM Student";//executes the SQL statement, if it is a query statement. Successful return of results and objects$reslut=$db->query ($sql);//determine if the return has content, execute successfullyif($reslut){//If successful, the data is found from the result set object;   $attr=$reslut-Fetch_all ();
//returns an indexed array: $attr = $reslut->fetch_row ()//Returns an associative array: Tch_assoc ();//return object: $attr = $reslut->fetch_object ();// Returns both indexed and associated: $ARRT = $reslut->fetch_array (); Var_dump($attr);?>

The last return value is a set object, we use Fetch_all (), Fetch_row () in order to see the results clearly. Fetch_object ();

Add or delete the statement: added a message:
//Connecting Objects$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//Preparing SQL statements$sql= "INSERT into cou values (' 3-111 ', ' hehe ', ' 803 ')"; //Execute SQL statement$r=$db->query ($sql);if($r)//if "is Ture{    Echo"Successful Execution";}Else {    Echo"Execution Failed";}

Changing a message:
<?PHP//Connecting Objects$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//Preparing SQL statements$sql= "Update cou set cname= ' haha ' where tno= ' 803 '"; //Execute SQL statement$r=$db->query ($sql);if($r){    Echo"Successful Execution";}Else {    Echo"Execution Failed";}?>
Delete:
<?PHP//Connecting Objects$db=NewMysqli ("localhost", "root", "12345678", "Heiheihei");//Preparing SQL statements$sql= "Delete from cou where tno= ' 803 '"; //Execute SQL statement$r=$db->query ($sql);if($r){    Echo"Successful Execution";}Else {    Echo"Execution Failed";}?>

PHP connection MySQL Database

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.