"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > IN 연산자와 함께 MySQLI 준비된 진술을 효과적으로 사용하는 방법은 무엇입니까?

IN 연산자와 함께 MySQLI 준비된 진술을 효과적으로 사용하는 방법은 무엇입니까?

2025-03-23에 게시되었습니다
검색:102

How to Use MySQLi Prepared Statements with the IN Operator Effectively?

The Problem

As mentioned in the initial query, binding a comma-separated string of values ​​to the prepared statement using bind_param('s', $in_statement) will not yield results, even though the data exists in the 데이터베이스.

솔루션

솔루션은 각 매개 변수 값을 개별적으로 처리하는 데 있습니다. type.

데이터 유형 문자열을 물음표 배열과 결합하여 준비된 문에 대한 매개 변수 문자열을 형성합니다. 명령문.

예제 코드
  1. php $ lastNames =
  2. ; $ arparams = []; foreach ($ lastnames as $ key => $ value) { $ arparams [] = & $ lastNames [$ key]; } $ count_params = count ($ arparams); $ int = str_repeat ( 'i', $ count_params); Array_unshift ($ arparams, $ int); $ q = array_fill (0, $ count_params, '?'); $ params = implode ( ',', $ q); $ data_res = $ mysqli-> repary ( "``id`,`name`,`age '에서'age ''에서`lastname '에서``lastname' '({$ params})"); call_user_func_array ([$ data_res, 'bind_param'], $ arparams); $ data_res-> execute (); $ result = $ data_res-> get_result (); while ($ data = $ result-> fetch_array (mysqli_assoc)) { // 여기에서 코드 ... } $ result-> free (); $ data_res-> close ();
  3. 이 접근 방식은 IN 연산자를 사용하여 준비된 명령문에 여러 값을 올바르게 바인딩 할 수 있습니다.
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3