php 정의되지 않은 변수..

php 정의되지 않은 변수..

QA

php 정의되지 않은 변수..

본문

구글맵에 마커찍는 거 하고 있는데 mainpage.php에서 $data['idx'], $data['lng'], $data['lat'] 가 정의되지 않은변수라고 오류가 나오네요... lib.php파일에 db연동하는 코드 넣었구 mysql에도 lat필드랑 lng필드에 값 다 넣었는데 왜 오류가 나는걸까요..? 정말 모르겠습니다.. 혹시 몰라 lib.php 파일도 올리겠습니다.

lib.php(DB연동 부분)


<?
 
    error_reporting(1);
    ini_set("display_errors", 1);

 
    $connect = mysqli_connect("localhost", "root", "1234", "carsharing") or die("MySQL 접속 실패!! ");
 
    if(mysqli_connect_error()){
        echo "mysql 접속중 오류가 발생했습니다. ";
        echo mysqli_connect_error();
    }

mainpage.php

<?
  include "lib.php";
?>
 
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <style type="text/css">
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
 
      /* Optional: Makes the sample page fill the window. */
      html,
      body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
    <script>
      let map;
 
      function initMap() {
        map = new google.maps.Map(document.getElementById("map"), {
          center: { lat: 37.67606384745595, lng: 126.74730456163915 },
          zoom: 15,
        });
 
    const myLatLng = { lat: 37.67606384745595, lng: 126.74730456163915 };
 
    new google.maps.Marker({
    position: myLatLng,
    map,
    title: "Hello World!",
 
  });
 
  <?
  $query = "select * from map ";
  $result = mysqli_query($connect, $query);
 
  while($data = mysqli_fetch_array($result)){
  ?>
    const myLatLng<?=$data['idx']?> = { lat: <?=$data['lat']?>, lng: <?=$data['lng']?> }; //오류
 
    new google.maps.Marker({
    position: myLatLng<?=$data['idx']?>, //오류 Undefined variable: data in <b>C:\xampp\htdocs\mainpage.php</b> on line <b>49</b><br />
    map,
    title: "<=$data['title']?>",
    });
<? } ?>
      }
    </script>
  </head>
  <body>
    <div id="map"></div>
 
    <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyADE6zw1xt9mEbQYvRuJS7_rGDX5Su0Et0&callback=initMap&libraries=&v=weekly"
      async
    ></script>
  </body>
</html>

이 질문에 댓글 쓰기 :

답변 6

 const myLatLng<?=$data['idx']?> = { lat: <?=$data['lat']?>, lng: <?=$data['lng']?> }; //오류
 

브라우저 소스보기"에서 위 부분이 어떻게 나타나나요?

<b>Notice</b>:  Undefined variable: data in <b>C:\xampp\htdocs\mainpage.php</b> on line <b>49</b><br />
 = { lat: <br />
<b>Notice</b>:  Undefined variable: data in <b>C:\xampp\htdocs\mainpage.php</b> on line <b>49</b><br />
, lng: <br />
<b>Notice</b>:  Undefined variable: data in <b>C:\xampp\htdocs\mainpage.php</b> on line <b>49</b><br />
 };
 이렇게 나옵니다 ㅠㅠ 변수 세개를 다 인식을 못하는것같아요

db table "map"에

idx, lat,lng
 

이런 컬럼이 있는걸까요?

데이터베이스 연동이 안되는거 같네요. 연동되는지 확인부터 필요합니다.

while($data = mysqli_fetch_array($result)) 도달하기 전에

먼저 print_r($result) 로 먼저 확인한번 해보세요

 


 

감사합니다 해결했습니다!

 

답변을 작성하시기 전에 로그인 해주세요.
전체 0 | RSS
QA 내용 검색
  • 개별 목록 구성 제목 답변작성자조회작성일
  • 질문이 없습니다.

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT