oci_define_by_name

(PHP 5, PHP 7, PHP 8, PECL OCI8 >= 1.1.0)

oci_define_by_name — 쿼리 가져오기를 위한 열과 PHP 변수 연결


설명

oci_define_by_name(
    resource $statement,
    string $column,
    mixed &$var,
    int $type = 0
): bool
                

oci_fetch()를 사용하여 쿼리 가져오기를 위한 열과 PHP 변수를 연결합니다.

oci_define_by_name() 호출은 oci_execute()를 실행하기 전에 발생해야 합니다.


매개변수

statement
oci_parse()에 의해 생성되고 oci_execute()에 의해 실행되는 유효한 OCI8 문 식별자 또는 REF CURSOR 문 식별자.
column
쿼리에 사용된 열 이름입니다.

대소문자를 구분하지 않는 Oracle의 기본 열 이름에는 대문자를 사용하십시오. 대소문자를 구분하는 열 ​​이름의 경우 정확한 열 이름 대소문자를 사용합니다.

var
반환된 열 값을 포함할 PHP 변수입니다.
type
반환할 데이터 유형입니다. 일반적으로 필요하지 않습니다. Oracle 스타일 데이터 변환은 수행되지 않습니다. 예를 들어, SQLT_INT는 무시되고 반환된 데이터 유형은 여전히 ​​SQLT_CHR입니다.

선택적으로 oci_new_descriptor()를 사용하여 LOB/ROWID/BFILE 설명자를 할당할 수 있습니다.


반환 값

성공하면 true를, 실패하면 false를 반환합니다.


Examples

예제 #1 oci_define_by_name() 예제

                  
<?php

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$sql = 'SELECT location_id, city FROM locations WHERE location_id < 1200';
$stid = oci_parse($conn, $sql);

// The defines MUST be done before executing
oci_define_by_name($stid, 'LOCATION_ID', $locid);
oci_define_by_name($stid, 'CITY', $city);

oci_execute($stid);

// Each fetch populates the previously defined variables with the next row's data
while (oci_fetch($stid)) {
    echo "Location id $locid is $city<br>\n";
}

// Displays:
//   Location id 1000 is Roma
//   Location id 1100 is Venice

oci_free_statement($stid);
oci_close($conn);

?>
                  
                

예제 #2 대소문자를 구분하는 열 ​​이름이 있는 oci_define_by_name()

                 
<?php

/*
  Before running, create the table with a case sensitive column name:
    CREATE TABLE mytab (id NUMBER, "MyDescription" VARCHAR2(30));
    INSERT INTO mytab (id, "MyDescription") values (1, 'Iced Coffee');
    COMMIT;
*/

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT * FROM mytab');

// Use uppercase for non case-sensitive column names
oci_define_by_name($stid, 'ID', $id);

// Use the exact case for case-sensitive column names
oci_define_by_name($stid, 'MyDescription', $mydesc);

oci_execute($stid);

while (oci_fetch($stid)) {
    echo "id $id is $mydesc<br>\n";
}

// Displays:
//   id 1 is Iced Coffee

oci_free_statement($stid);
oci_close($conn);

?>
                   
                 

예제 #3 LOB 열이 있는 oci_define_by_name()

                  
 <?php

/*
  Before running, create the table:
    CREATE TABLE mytab (id NUMBER, fruit CLOB);
    INSERT INTO mytab (id, fruit) values (1, 'apple');
    INSERT INTO mytab (id, fruit) values (2, 'orange');
    COMMIT;
*/

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT * FROM mytab');

// The defines MUST be done before executing
oci_define_by_name($stid, 'ID', $id);
oci_define_by_name($stid, 'FRUIT', $fruit);  // $fruit will become a LOB descriptor

oci_execute($stid);

while (oci_fetch($stid)) {
    echo $id . " is " . $fruit->load(100) . "<br>\n";
}

// Displays:
//   1 is apple
//   2 is orange

$fruit->free();
oci_free_statement($stid);
oci_close($conn);

?>
                    
                  

예제 #4 명시적 유형의 oci_define_by_name()

                   
  <?php

  /*
    Before running, create the table:
      CREATE TABLE mytab (id NUMBER, fruit CLOB);
      INSERT INTO mytab (id, fruit) values (1, 'apple');
      INSERT INTO mytab (id, fruit) values (2, 'orange');
      COMMIT;
  */

  $conn = oci_connect('hr', 'welcome', 'localhost/XE');
  if (!$conn) {
      $e = oci_error();
      trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
  }

  $stid = oci_parse($conn, 'SELECT * FROM mytab');

  // The defines MUST be done before executing
  oci_define_by_name($stid, 'ID', $id);

  $fruit = oci_new_descriptor($conn, OCI_D_LOB);
  oci_define_by_name($stid, 'FRUIT', $fruit, OCI_D_CLOB);

  oci_execute($stid);

  while (oci_fetch($stid)) {
      echo $id . " is " . $fruit->load(100) . "<br>\n";
  }

  // Displays:
  //   1 is apple
  //   2 is orange

  $fruit->free();
  oci_free_statement($stid);
  oci_close($conn);

  ?>
                     
                   

기타

  • oci_fetch() - 쿼리에서 다음 행을 내부 버퍼로 가져옵니다.
  • oci_new_descriptor() - 새로운 빈 LOB 또는 FILE 설명자를 초기화합니다.