728x90
반응형

PHP 91

[PHP] Invalid argument supplied for foreach() 에러

■ PHP Invalid argument supplied for foreach() 에러 배열을 foreach 문 돌릴 때 해당 에러가 발생하는 경우가 있음 배열 내 공백을 가진 변수가 존재할 경우 발생하는 에러이기 때문에 foreach문을 돌리기 전 해당 배열이 빈 배열인지 체크하는 코드를 추가해주면 됨 ■ 해결 방법 $test_arr 이라는 배열을 foreach 문 돌린다고 가정하였을 때 if( !empty($test_arr) ){ foreach( $test_arr as $target ){ } }

PHP 2022.04.29

[PHP] date() 출력 시간이랑 서버 시간이 안맞을때 (timezone)

■ PHP date() 출력 시간이랑 서버 시간이 안맞을때 (timezone) PHP 에서 date() 함수 사용 시 출력되는 시간이랑 서버 시간이 다르게 나올 경우 date() 함수 사용 전 상단에 date_default_timezone_set() 함수를 사용해 timezone 설정을 해주면 됨 ■ 사용법 date_default_timezone_set('Asia/Seoul'); echo date("Y-m-d H:i:s");// 2022-04-25 08:05:36 (현재 시간과 상이하게 출력됨) date_default_timezone_set('Asia/Seoul'); echo date("Y-m-d H:i:s");// 2022-04-25 17:04:34 (현재 시간과 동일하게 출력됨)

PHP 2022.04.26

[PHP] json_encode JSON 데이터 내 역슬래시 제거 str_replace

■ PHP json_encode JSON 데이터 내 역슬래시 제거 json_encode() 처리를 할 때 데이터 내 슬래시가 존재할 경우 슬래시가 역슬래시와 함께 출력됨 이 때 역슬래시(백슬래시, \) 제거하는 방법 // $testArr 배열 출력 {"testArr":[{"testUrl":"https://www.gentlemonster.com/shop/ver1_detail.php?it_id=1629261901"}]} // $testArr json_encode() 처리 echo json_encode($testArr); {"testArr":[{"testUrl":"https:\/\/www.gentlemonster.com\/shop\/ver1_detail.php?it_id=1629261901"}]} ■ 역슬래시..

PHP 2022.04.25

[PHP] 최근 MySQL 작업으로 변경된 행 개수 구하기, mysql_affected_rows

■ PHP 최근 MySQL 작업으로 변경된 행 개수 구하기, mysql_affected_rows mysql_affected_rows INSERT, UPDATE, REPLACE, DELETE 질의로 영향받은 행 수를 얻을 때 사용하는 함수로 실패 시 "-1" 반환, 성공 시 적용된 행의 개수 반환 ■ 설명 mysql_affected_rows(resource $link_identifier = NULL): int ■ 사용법 $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('mydb'); /* this s..

PHP 2022.02.25

[PHP] PHP에서 .CSV 로 내보내기, php excel .csv file download

■ PHP php excel .csv file download PHP를 이용해 MySQL DB(데이터베이스)의 데이터 자체를 CSV 파일로 내보내야 하는 경우가 있음 ■ 예제 PHP를 이용해 CSV 파일 형태로 내보내는 예제 // 파일명 지정 $filename = "Example_Filename_" . date("Ymd") . ".csv"; header('Content-Type:text/css;charset=EUC-KR;'); header('Expires: 0'); header('Content-Transfer-Encoding: binary'); header('Cache-Control: private, no-transform, no-store, must-revalidate'); header("Content..

PHP 2022.02.24

[PHP] php_writeexcel 이용 .xls 파일 생성

■ PHP php_writeexcel 이용 .xls 파일 생성 PHP에서 .xls 파일 생성 시 대부분 PHPExcel을 사용하지만 최소 php 5.2 이상이어야 가능하기 때문에 다른 라이브러리를 사용해야할 때가 있음 많은 라이브러리 중 php_writeexcel 에 대한 사용법을 정리함 ■ php_writeexcel http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/ bettina-attack.de www.bettina-attack.de ■ 사용법 배열에 저장된 값에 문자 변환 함수 적용을 위해 array_map 함수 사용함

PHP 2022.02.17
728x90
반응형