> 데이터 베이스 > MySQL 튜토리얼 > MySQLi를 사용하여 PHP에서 준비된 명령문 사용을 어떻게 최적화할 수 있습니까?

MySQLi를 사용하여 PHP에서 준비된 명령문 사용을 어떻게 최적화할 수 있습니까?

Susan Sarandon
풀어 주다: 2024-12-19 09:31:12
원래의
966명이 탐색했습니다.

How Can I Optimize Prepared Statement Usage in PHP with MySQLi?

MySQLi를 사용하는 PHP에서 최적화된 준비된 명령문 사용

귀하의 코드에 준비된 명령문에 대한 매개변수 바인딩이 누락된 것 같습니다. mysqli::prepare 문서에 따르면 명령문을 실행하기 전에 매개변수 표시자를 바인딩해야 합니다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

// Bind parameters for the first insertion

$name1 = 'one';

$age1  = 1;

$stmt->bind_param('si', $name1, $age1);

 

// Execute for the first insertion

$stmt->execute();

 

// Bind parameters for the second insertion

$name2 = 'two';

$age2  = 1;

$stmt->bind_param('si', $name2, $age2);

 

// Execute for the second insertion

$stmt->execute();

로그인 후 복사

mysqli의 장점

mysqli가 유일한 라이브러리는 아닙니다. 준비된 문을 지원하는 이 프로그램은 여러 가지를 제공합니다. 장점:

  • 향상된 보안: 준비된 명령문은 임의 SQL 쿼리 실행을 방지하여 SQL 삽입 공격으로부터 보호합니다.
  • 향상된 성능: 준비된 문은 구문 분석된 쿼리를 재사용하여 효율성을 높입니다.
  • 오류 처리: 준비된 문은 더 나은 오류 보고 및 처리 기능을 제공합니다.

오류 처리가 포함된 전체 예

다음은 준비된 문을 사용하는 전체 예입니다. 오류 처리를 포함한 MySQLi 기반 PHP:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

// Database connection

$mysqli = new mysqli("localhost", "root", "root", "test");

 

if ($mysqli->connect_errno) {

    echo "Failed to connect: " . $mysqli->connect_error;

    exit;

}

 

// Prepare statement

$stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?,?)");

 

if (!$stmt) {

    echo "Prepare failed: " . $mysqli->error;

    exit;

}

 

// Bind parameters for the first insertion

$name1 = 'one';

$age1  = 1;

$stmt->bind_param('si', $name1, $age1);

 

// Execute for the first insertion

$stmt->execute();

 

if ($stmt->errno) {

    echo "Execution failed: " . $stmt->error;

    exit;

}

 

// Bind parameters for the second insertion

$name2 = 'two';

$age2  = 1;

$stmt->bind_param('si', $name2, $age2);

 

// Execute for the second insertion

$stmt->execute();

 

if ($stmt->errno) {

    echo "Execution failed: " . $stmt->error;

    exit;

}

 

// Selection statement

$stmt = $mysqli->prepare("SELECT * FROM users WHERE age = ?");

 

if (!$stmt) {

    echo "Prepare failed: " . $mysqli->error;

    exit;

}

 

// Bind parameters for selection

$age  = 1;

$stmt->bind_param('i', $age);

 

// Execute selection

$stmt->execute();

 

if ($stmt->errno) {

    echo "Execution failed: " . $stmt->error;

    exit;

}

 

// Fetch results

$result = $stmt->get_result();

 

if (!$result) {

    echo "Fetch failed: " . $stmt->error;

    exit;

}

 

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

    echo $row['name'] . " " . $row['age'] . "<br>";

}

로그인 후 복사

위 내용은 MySQLi를 사용하여 PHP에서 준비된 명령문 사용을 어떻게 최적화할 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿