How PHP connects to a database

Source: Internet
Author: User
This article introduces to you about how PHP connects to the database, has a certain reference value, the need for friends can refer to.

1. PHP Link Database:

1. Link Database

2. Determine if the connection is successful

3. Set character sets

4. Select Database

5. Preparing SQL statements

6. Send SQL statements

7. Processing result Sets

8. Releasing resources (Close database)

$result = Mysqli_querry ($link, $sql)//Returns an object

MYSQLI_FETCH_ASSOC ($result) a one-dimensional associative array that reads down and returns

Mysqli_fetch_row ($result) returns an indexed array

Mysqli_fetch_array ($result) returns an indexed and associated array

Mysqli_num_rows ($result) The total number of results set when the query is returned

Mysqli_affected_rows ($link) returns the number of rows affected when you modify, delete, add

MYSQLI_INSERT_ID ($link) returns the self-increment ID of the current data that you inserted

<<?php    $link = mysqli_connect (' localhost ', ' root ', ');    Var_dump ($link);    1. Connect database    if (! $link) {    exit (' Connect database failed ');}    2, determine whether the database is connected successfully    Mysqli_set_charset ($link, ' utf8 ');    3, set character sets    mysqli_select_db ($link, ' BBS ');    4. Select database    $sql = "SELECT * from Bbs_user";    5. Prepare SQL statement    $res = Mysqli_query ($link, $sql);    6. Send SQL statement    $result = MYSQLI_FETCH_ASSOC ($res);    $result = Mysqli_fetch_assoc ($res);    7, processing result set    mysqli_close ($link);    8. Close the database?>

This returns an associative array.

Output all arrays: (with loops)

<?php    $link = mysqli_connect (' localhost ', ' root ', ');    if (! $link) {    exit ($ ' failed to connect to database ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "SELECT * from Bbs_user";    $res = Mysqli_query ($link, $sql);    while ($result = Mysqli_fetch_assoc ($res)) {    var_dump ($result);}    Mysqli_close ($link);? >


Output an array of indexes:

<?php    $link = mysqli_connect (' localhost ', ' root ', ');    if (! $link) {    exit (' failed to connect to database ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "SELECT * from Bbs_user";    $res = Mysqli_query ($link, $sql);    $result = Mysqli_fetch_row ($res); Var_dump ($result);    Mysqli_close ($link);? >

That is, the output associative array, and the output index array:

<?php    $link = mysqli_connect (' localhost ', ' root ', ');    if (! $link) {    exit (' failed to connect to database ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "SELECT * from Bbs_user";    $res = Mysqli_query ($link, $sql);    $result = Mysqli_fetch_array ($res); Var_dump ($result);    Mysqli_close ($link);? >

Total query data:

<?php    $link = mysqli_connect (' localhost ', ' root ', ');    if (! $link) {    exit (' failed to connect to database ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "SELECT * from Bbs_user";    $obj = Mysqli_query ($link, $sql);    $res = Mysqli_num_rows ($obj);    Var_dump ($res);    Mysqli_close ($link);? >

Insert new data in PHP:

<?php    $link = mysqli_connect (' localhost ', ' root ', ');    if (! $link) {    exit (' failed to connect to database ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "INSERT INTO Bbs_user values (9, ' KKK ', ' 789789 ', ' Nanjian ', 2,15)";    $obj = Mysqli_query ($link, $sql);    $res = mysqli_insert_id ($link);    Var_dump ($res);    Mysqli_close ($link);? >


<?php    $link = mysqli_connect (' Lcoalhost ', ' root ', ');    if (! $link) {    exit (' link database failed ');}    Mysqli_set_charset ($link, ' UTF8 ';)    mysqli_select_db ($link, ' BBS ');    $sql = "SELECT * from Bbs_user";    $obj = Mysqli_query ($link, $sql);    Echo ' <th> number </th><th> user name </th><th> address </th><th> gender </th><th> age </th> ';    while ($res = Mysqli_fetch_assoc ($obj)) {    echo ' <tr> ';       Echo ' <td> ' $res [' id ']. ' </td> ';          Echo ' <td> ' $res [' username ']. ' </td> ';       Echo ' <td> ' $res [' address ']. ' </td> ';       Echo ' <td> ' $res [' sex ']. ' </td> ';       Echo ' <td> ' $res [' age ']. ' </td> ';       Echo ' <td><a href= ' del.php?id= '. $res [' id ']. ' " > Delete </a>/<a href= "update.php?id=". $res [' id ']. ' " > Modify </a></td> ';    Echo ' </tr> ';}        ? >

To compile the delete php file: (del.php)

<?php    $id =$_get[' id '];    $link = Mysqli_connect (' localhost ', ' root ', ');    if (! $link) {    exit (' failed to connect to database ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "Delete from Bbs_user where id= $id";    $boolearn = Mysqli_query ($link, $sql);    if ($boolearn && msyqli_affected_rows ($link)) {    echo ' delete succeeded ';} else {    echo ' delete failed ';}    Mysqli_close ($link);? >

To compile the modified PHP file: (update.php)

<?php $id = $_get[' id '];    $link = Mysqli_connect (' localhost ', ' root ', ');    if (! $link) {exit (' failed to connect to database ');}    Mysqli_set_charset ($link, ' UTF8 ');    msyqli_select_db ($link, ' BBS ');    $sql = "SELECT * from Bbs_user where id= $id";    $obj = Mysqli_query ($link, $sql); $rows = Mysqli_fetch_assoc ($obj);? >

doupdate.php:

1 <?php2     var_dump ($_GRT); 3?>

doupadate.php

<?php    $id = $_get[' id '];    $username = $_get[' username ');    $address = $_get[' adress ');    $sex = $_get[' sex '];    $age = $_get[' age '];    $link = Mysqli_connect (' Lcoalhost ', ' root ', ');    if (! $link) {    exit (' Database connection failed ');}    Mysqli_set_charset ($link, ' utf8 ');    mysqli_select_db ($link, ' BBS ');    $sql = "Update bbs_user set username= ' $username ', address= ' $address ',     sex= ' $sex ', age= ' $age ' where id= ' $id '";    $res = Mysqli_query ($link, $sql);    if ($res && mysqli_affected_rows ($link)) {    echo ' modified successfully <a href= ' update.php ' > Return </a> ';}    else {    echo ' modification failed ';}    Mysqli_close ($link);? >

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.