search

mysql_assoc

Jun 13, 2016 am 10:50 AM
arrayfetchfunctionmysqlphpquerytype

[php] 
function fetch_array($query, $result_type = MYSQL_ASSOC) 
 

        return mysql_fetch_array($query, $result_type); 

      可能在看PHP项目的时候,特别是和数据库连用的时候,一般的代码都会出现这样的形式,只是相当于函数接口的转换。

      其中有MYSQL_ASSOC一下子没看懂是什么,后来查了下手册,竟然是泛泛的谈了下:

      mysql_fetch_array() 中可选的第二个参数 result_type 是一个常量,可以接受以下值:MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH。本特性是 PHP 3.0.7 起新加的。本参数的默认值是 MYSQL_BOTH。

      这让我有点小纠结,后来百度了下,终于知道区别了。其中MYSQL_ASSOC是只能用关联索引,MYSQL_NUM只能用数字索引,MYSQL_BOTH数字、关联都是可以的。(ps:这里不知道用索引合不合适,其实也就是数组的KEY了)

      下面是三段手册上的代码:

     EXAMPLE1 MYSQL_NUM

[php] 
    mysql_connect("localhost", "mysql_user", "mysql_password") or 
        die("Could not connect: " . mysql_error()); 
    mysql_select_db("mydb"); 
 
    $result = mysql_query("SELECT id, name FROM mytable"); 
 
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
        printf ("ID: %s  Name: %s", $row[0], $row[1]); 
    } 
 
    mysql_free_result($result); 
?>  

    EXAMPLE2 MYSQL_ASSOC

[php] 
    mysql_connect("localhost", "mysql_user", "mysql_password") or 
        die("Could not connect: " . mysql_error()); 
    mysql_select_db("mydb"); 
 
    $result = mysql_query("SELECT id, name FROM mytable"); 
 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
        printf ("ID: %s  Name: %s", $row["id"], $row["name"]); 
    } 
 
    mysql_free_result($result); 
?>  
    EXAMPLE MYSQL_BOTH

[php] 
    mysql_connect("localhost", "mysql_user", "mysql_password") or 
        die("Could not connect: " . mysql_error()); 
    mysql_select_db("mydb"); 
 
    $result = mysql_query("SELECT id, name FROM mytable"); 
 
    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { 
        printf ("ID: %s  Name: %s", $row[0], $row["name"]); 
    } 
 
    mysql_free_result($result); 
?>  

其中MYSQL_BOTH
[php]
printf ("ID: %s  Name: %s", $row["id"], $row["name"]); 
lt;pre name="code" class="php"> printf ("ID: %s  Name: %s", $row["id"], $row[1]); 

作者:wolinxuebin
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

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 Tools

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools