라이브러리

[PHP] SqlStatement::getResult - 결과 얻기




PHP에서 SqlStatement::getResult에 대한 설명


PHP에서 SqlStatement::getResult는 SQL 명령을 실행한 결과를 반환하는 메서드입니다. 이 메서드는 SQL 명령을 실행한 후 결과를 처리하기 위해 사용됩니다.

SqlStatement::getResult 메서드의 사용법


SqlStatement::getResult 메서드는 다음과 같은 형태로 사용됩니다.

#hostingforum.kr
php

$result = $statement->getResult();



예제


다음 예제는 MySQL에 연결하여 SELECT 명령을 실행한 후 결과를 반환하는 예제입니다.

#hostingforum.kr
php

// MySQL에 연결

$dsn = 'mysql:host=localhost;dbname=mydb';

$username = 'myuser';

$password = 'mypassword';



try {

    $pdo = new PDO($dsn, $username, $password);

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

}



// SQL 명령을 실행

$stmt = $pdo->prepare('SELECT * FROM users');

$stmt->execute();



// 결과를 반환

$result = $stmt->getResult();



// 결과를 출력

while ($row = $result->fetch()) {

    echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'] . "
";

}



결과 반환 방법


SqlStatement::getResult 메서드는 결과를 반환하기 위해 다음 방법 중 하나를 사용할 수 있습니다.

* `fetch()` 메서드: 결과를 한 행씩 반환합니다.
* `fetchAll()` 메서드: 결과를 모두 반환합니다.
* `fetchObject()` 메서드: 결과를 객체로 반환합니다.

예제 (fetch() 메서드)


다음 예제는 MySQL에 연결하여 SELECT 명령을 실행한 후 결과를 한 행씩 반환하는 예제입니다.

#hostingforum.kr
php

// MySQL에 연결

$dsn = 'mysql:host=localhost;dbname=mydb';

$username = 'myuser';

$password = 'mypassword';



try {

    $pdo = new PDO($dsn, $username, $password);

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

}



// SQL 명령을 실행

$stmt = $pdo->prepare('SELECT * FROM users');

$stmt->execute();



// 결과를 반환

while ($row = $stmt->fetch()) {

    echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'] . "
";

}



예제 (fetchAll() 메서드)


다음 예제는 MySQL에 연결하여 SELECT 명령을 실행한 후 결과를 모두 반환하는 예제입니다.

#hostingforum.kr
php

// MySQL에 연결

$dsn = 'mysql:host=localhost;dbname=mydb';

$username = 'myuser';

$password = 'mypassword';



try {

    $pdo = new PDO($dsn, $username, $password);

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

}



// SQL 명령을 실행

$stmt = $pdo->prepare('SELECT * FROM users');

$stmt->execute();



// 결과를 반환

$result = $stmt->fetchAll();

foreach ($result as $row) {

    echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'] . "
";

}



예제 (fetchObject() 메서드)


다음 예제는 MySQL에 연결하여 SELECT 명령을 실행한 후 결과를 객체로 반환하는 예제입니다.

#hostingforum.kr
php

// MySQL에 연결

$dsn = 'mysql:host=localhost;dbname=mydb';

$username = 'myuser';

$password = 'mypassword';



try {

    $pdo = new PDO($dsn, $username, $password);

} catch (PDOException $e) {

    echo 'Connection failed: ' . $e->getMessage();

}



// SQL 명령을 실행

$stmt = $pdo->prepare('SELECT * FROM users');

$stmt->execute();



// 결과를 반환

$result = $stmt->fetchObject();

echo $result->id . ' ' . $result->name . ' ' . $result->email . "
";



결론


SqlStatement::getResult 메서드는 SQL 명령을 실행한 결과를 반환하는 메서드입니다. 이 메서드는 결과를 처리하기 위해 사용됩니다. 결과를 반환하는 방법은 `fetch()`, `fetchAll()`, `fetchObject()` 메서드를 사용할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 130 페이지

검색

게시물 검색