search
HomeBackend DevelopmentPHP TutorialSome summary and detailed introduction about php mysqli function (5)

Foreword: PHP is a programming language that is relatively easy to get started with, and PHP has a lot of built-in functions. Therefore, it is particularly important to understand and master these built-in functions. Next we will analyze some of the built-in functions of PHP.

I will continue to organize it for you in the future.

Recommended related PHP video tutorials: https://www.php.cn/course/list/29/type/2.html

Understanding of phpMysqli functions :

1. What is php mysqli?

php mysqli = php nysqli improved

The mysqli function allows you to access the database server.

Notice! The mysqli extension is available for mysqli version 4.1.13 or newer.

2. How to use mysqli function?

If you want to use the mysqli function, you must add support for the mysqli extension when compiling php.

For more information about installation, please visit: http://www.php.net/manual/en/mysqli.installation.php

For details about running configuration Detailed information address: http://www.php.net/manual/en/mysqli.configuration.php

3. Introduction to php related functions

1, mysqli_fetch_array()Function

Description: Fetch a row from the result set as a numeric number or associative array, or both.

Note: The field names returned by this function are strictly case-sensitive.

2, mysqli_fetch_assocFunction

Description: Take a row from the result set as an associative array.

Note: The field names returned by this function are strictly case-sensitive.

3, mysqli_fetch_field_direct(result,fieldnr)Function

The parameter fieldnr is required, and the field number is specified to be between 0 and the number of fields-1 between.

Description: Take the meta-data of a single field (column) from the result set and return it as an object.

Example:

<?php
//配置数据库信息
$localhost = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接数据库失败!&#39;.mysqli_connect_error());
}
//定义sql语句
$sql = "SELECT * FROM demo";
if($result = mysqli_query($conn, $sql)) {
//获取字段“age”的信息
$fieldinfo = mysqli_fetch_field_direct($result, 2);
printf("字段名:%s",$fieldinfo->name);
echo "<br>";
printf("数据表:%s",$fieldinfo->table);
echo "<br>";
printf("最大长度:%s",$fieldinfo->max_length);
//释放结果集
mysqli_free_result($result);
}

//关闭连接
mysqli_close($conn);
?>

What should be noted here is that the return value is an object containing the definition information of the field. If there is no available information, false is returned. This return object has the following attributes.

>name -Field name

>orgname -Original field name (if the field has an alias specified)

>table -The name of the table to which the field belongs

>orgtable -The original table name (if an alias is specified)

> ;def -The default value of the field

>max_length-The maximum width of the field

>length - The field width specified in the table definition

>charsetnr -The character set number of the field

>flags -The bit flags of the field

>type - The data type used for the field

>decimals - Integer field, the number of digits after the decimal point

4, mysqli_fetch_field()Function

Description: Get the next field from the result set and return related information.

Example:

<?php
//配置数据库信息
$localhost = &#39;localhost&#39;;
$username = &#39;zmz&#39;;
$password = &#39;20040315&#39;;
$dbname = &#39;zmz&#39;;
$port = 3306;

//连接数据库
$conn = mysqli_connect($localhost,$username,$password,$dbname,$port);
//检查连接
if(mysqli_connect_errno($conn)) {
die(&#39;连接数据库失败!&#39;.mysqli_connect_error());
}
//定义sql语句
$sql = "SELECT * FROM demo";
if($result = mysqli_query($conn, $sql)) {
//获取字段“age”的信息
$fieldinfo = mysqli_fetch_field($result);
printf("字段名:%s",$fieldinfo->name);
echo "<br>";
printf("数据表:%s",$fieldinfo->table);
echo "<br>";
printf("最大长度:%s",$fieldinfo->max_length);
//释放结果集
mysqli_free_result($result);
}

//关闭连接
mysqli_close($conn);
?>

The returned object properties are the same as above.

5, mysqli_fetch_fields()Function

Description: Return an array of objects representing fields in the result set, and then output related information.

Object properties are the same as above.

The above are the functions introduced to you this time. I hope it will be helpful to you. Thanks!

Recommended related articles: https://www.php.cn/php-weizijiaocheng-428673.html

The above is the detailed content of Some summary and detailed introduction about php mysqli function (5). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.