PHP16 PHP Access MySQL

Source: Internet
Author: User
Tags configuration php

Learning Essentials
    • PHP Access MySQL Configuration
    • PHP Access MySQL function introduction
    • Football Schedule Information Management

PHP access MySQL configuration php.ini configuration file confirm that the following configuration is turned on

Extension=php_mysql.dll PHP5 before accessing the database module

Extension=php_mysqli.dll PHP5 after accessing the database module

Get support information via GetInfo () to verify that PHP has supported MySQL access

PHP Access MySQL function introduction Mysqli_connect () function
    • Function description

Open a new connection to the MySQL server.

    • Syntax format

Mysqli_connect (Host,username,password,dbname,port,socket);

    • Parameter description

Parameters

Describe

Host

Optional. Specifies the host name or IP address.

Username

Optional. Specifies the MySQL user name.

Password

Optional. Specify the MySQL password.

dbname

Optional. Specifies the database that is used by default.

Port

Optional. Specifies the port number to attempt to connect to the MySQL server.

Socket

Optional. Specify the socket or named pipe to be used.

    • return value

Returns an object that represents the connection to the MySQL server, the resource type.

    • Sample code

$link =mysqli_connect (' localhost',' root ',' rootkit ' ,' MySchool ');

Description: MySQL default port 3306, the host address can omit the port number, or omit the port number parameter. So it can be written in the following form.

    

Mysqli_query () function
    • Function description

Executes a query against the database.

    • Syntax format

Mysqli_query (Connection,query,resultmode);

    • Parameter description

Parameters

Describe

Connection

Necessary. Specifies the MySQL connection to use.

Query

Required, specifies the query string.

Resultmode

Optional. A constant. Can be any one of the following values:

Ümysqli_use_result (Use this if you need to retrieve large amounts of data)

Ümysqli_store_result (default)

    • return value

A Mysqli_result object is returned for a successful SELECT, SHOW, DESCRIBE, or EXPLAIN query. Returns TRUE for other successful queries. If it fails, it returns FALSE.

Special Note : The function of asynchronous query is added in PHP 5.3.0.

    • Sample code
Build the SQL statement string $query= "SELECT * from student";//execute the SQL statement and return the array result set $result=mysqli_query ($link, $query);

  

Mysqli_num_rows () function
    • Function description

Returns the number of rows in the result set.

    • Syntax format

mysqli_num_rows (result);

    • Parameter description

Parameters

Describe

Result

Necessary. Specifies the result set identifier returned by Mysqli_query (), Mysqli_store_result (), or Mysqli_use_result ().

    • return value

Returns the number of rows in the result set.

    • Sample code
Gets the number of records $rownum=mysqli_num_rows ($result);

  

MYSQLI_FETCH_ASSOC () function
    • Function description

Fetch a result row as an associative array.

    • Syntax format

Array Mysqli_fetch_assoc (Mysqli_result $result);

    • Parameter description

Parameters

Describe

Result

Procedural style only:a result set identifier returned by Mysqli_query (), Mysqli_store_result () or Mysqli_use_result ().

    • return value

Returns an associative array of strings representing the fetched row in the result set, where each key in the array repres Ents the name of the one of the result set ' s columns or NULL if there is no more than rows in resultset.

If or more columns of the result has the same field names, the last column would take precedence. To access the other column (s) of the same name, either need to access the result with numeric indices by using Mysqli_ Fetch_row () or add alias names.

    • Sample code
Returns a row of records from the array result set, the return value is an associative array structure//mysqli_fetch_object ($result) returns an object $row=mysqli_fetch_assoc ($result); Echo $row [' Studentno ': ';//The element value is determined by the database field name echo $row [' Studentname ']. " <br> ";   ;

  

Mysqli_free_result () function
    • Function description

Releases the resulting result set memory.

    • Syntax format

Mysqli_free_result (Result)

    • Parameter description

Parameters

Describe

Result

Necessary. Specifies the result set identifier returned by Mysqli_query (), Mysqli_store_result (), or Mysqli_use_result ().

    • return value

no return value.

Mysqli_close () function
    • Function description

Close the previously opened database connection.

    • Syntax format

Mysqli_close (connection);

    • Parameter description

Parameters

Describe

Connection

Specifies the MySQL connection to be closed.

    • return value

Returns TRUE if successful, or FALSE if it fails.

Complete database access steps
    1. Establishing a data connection
    2. Building SQL statements
    3. Get result set
    4. Working with result sets
    5. Close the result set resource
    6. To close a database connection
Complete sample Code
    • Inquire
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "SELECT * from Teaminfo";//3, query $result = mysqli_query ($link, $query);//4, processing result set $rownum = Mysqli_n Um_rows ($result); Gets the number of rows for the result set for ($i = 0; $i < $rownum; $i + +) {$row = Mysqli_fetch_assoc ($result); echo $row [' ID ']. ' = = = '. $row [' Teamname ']. ' <br> ';} 5, release the result set Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);

  

    • Add to
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "INSERT into Teaminfo (teamname) VALUES (' Xiamen International Trade ')";//3, query $result = mysqli_query ($link, $query);//4, Process result set if ($result) {echo ' Add team information successfully! ‘;} else {echo ' added team info failed! '; Echo mysqli_error ($link);  Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);

  

    • Modify
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';}  2, design SQL statement $query = "UPDATE teaminfo SET teamname= ' xiamen Seven Wolves ' WHERE id=19";//3, query $result = mysqli_query ($link, $query);// 4. Process result set if ($result) {echo ' Update team information successfully! ‘;} else {echo ' update team info failed! '; Echo mysqli_error ($link);} 5. Release result set//Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);

  

    • Delete
1, establish and database connection $link = Mysqli_connect (' 127.0.0.1 ', ' root ', ' rootkit ', ' Football '); {$link = = null) {echo ' Database connection setup failed ';} 2, design SQL statement $query = "DELETE from Teaminfo WHERE id=17";//3, query $result = mysqli_query ($link, $query);//4, processing result set if ($res ult) {echo ' Delete team information successfully! ‘;} else {echo ' deleted team info failed! '; Echo mysqli_error ($link);} 5. Release result set//Mysqli_free_result ($result);//6, Close database connection mysqli_close ($link);

  

On-Machine exercise: 2018 FIFA World Cup Asia regional schedule information management

using mysqli implement the following use cases:

    1. Achieve landing
    2. Implement Add schedule information
    3. Implement Delete schedule information
    4. Implement Modify schedule information
    5. Pagination Display for schedule information

PHP16 PHP Access MySQL

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.