PHP

    mysql_fetch_assoc - 연관 배열로 결과 행을 반환
    • 조회수 6,121
    • 작성일 2009-02-05
    •  

    인출된 행에서 문자열의 연관 배열을 반환하거나 더 이상의 행이 없다면 FALSE를 반환한다.

    <?php

    $conn 
    mysql_connect("localhost""mysql_user""mysql_password"
    );

    if (!
    $conn
    ) {
        echo 
    "Unable to connect to DB: " mysql_error
    ();
        exit;
    }
      
    if (!
    mysql_select_db("mydbname"
    )) {
        echo 
    "Unable to select mydbname: " mysql_error
    ();
        exit;
    }

    $sql 
    "SELECT id as userid, fullname, userstatus 
            FROM   sometable
            WHERE  userstatus = 1"
    ;

    $result mysql_query($sql
    );

    if (!
    $result
    ) {
        echo 
    "Could not successfully run query ($sql) from DB: " mysql_error
    ();
        exit;
    }

    if (
    mysql_num_rows($result) == 0
    ) {
        echo 
    "No rows found, nothing to print so am exiting"
    ;
        exit;
    }

    while ($row mysql_fetch_assoc($result)) {
        echo 
    $row["userid"
    ];
        echo 
    $row["fullname"
    ];
        echo 
    $row["userstatus"
    ];
    }

    mysql_free_result($result
    );

    ?>