본문 바로가기

PHP

PHP mysqli_fetch_array의 이해

728x90

1. PHP  DB연결 및 쿼리 결과 도출 과정

<?php 
  $servername ="localhost"; //hostname을 의미
  $username="root";
  $password='';
  $dbname='DB';
  $conn = mysqli_connect($servername,$username,$password,$dbname,3306); //boolean 값을 반환
  
/* ************************************ [PHP DOC - mysqli_connect() ] ******************************* 
 *  mysqli_connect(                                                                                 *
 *   ?string $hostname = null,                                                                      *
 *   ?string $username = null,                                                                      *  
 *   ?string $password = null,                                                                      *  
 *   ?string $database = null,                                                                      *
 *   ?int $port = null,                                                                             *
 *   ?string $socket = null                                                                         *
 * ): mysqli|false                                                                                  *  
 ****************************************************************************************************/ 
  $conn = mysqli_connect($servername,$username,'',$dbname, 3306);  
  $all_user = "SELECT id FROM member;";   
  // 첫 번째 파라미터: mysqli (class), 두 번째 파라미터: 쿼리 절  
  $all_user_result = mysqli_query($conn, $all_user);
/* ************************************ [PHP DOC - mysqli_fetch_array() ] ****************************
   *   Fetches one row of data from the result set and returns it as an array.                        *
 * Each subsequent call to this function will return the next row within the result set,              *      
 * or null if there are no more rows.                                                                 *
 * > "결과 집합으로 부터 한 한 개의 데이터 로우를 가져온다. 그리고 그것을 배열로 반환다."                   *
 * > "이 함수의 각 다음의 호출은 결과 집합에서 다음 로우를 반환 또는 만약 더 이상의 row들이 없다면 null 반환"*
*******************************************************************************************************/
  while($user_row = mysqli_fetch_array($all_user_result)) {
      $userId=$user_row['id'];  
     echo "$userId <br />\n";
  } 

?>

2. DB

Mysql DB

3. 크롬 브라우저에서 결과

Chrome 브라우저에서 echo 값 도출

728x90

'PHP' 카테고리의 다른 글

PHP 세션  (2) 2023.11.28
PHP 배열  (2) 2023.11.28