php 정의되지 않은 변수..

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

lib.php(DB연동 부분)

Copy
<?

 

    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

Copy
<?

  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개

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

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

무엇이 문제였나 궁금하네요. :)

댓글을 작성하려면 로그인이 필요합니다.

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

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

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

 


 
로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

'map' 테이블이 없거나 'map' 테이블에 'idx', 'lng', 'lat' 칼럼이 없을 가능성이 큽니다.

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

while($data = mysqli_fetch_array($result)){
  ?>

이 부분에서

 

 

while($data = mysqli_fetch_array($result)){
   print_r($data);
  ?>

를 해보세요

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

db table "map"에

idx, lat,lng
 

이런 컬럼이 있는걸까요?

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

네 컬럼들 다 만들어 놓고 테스트용으로 데이터도 하나 넣어놓았습니다... 혹시 db연동이 안되고 있어서 그럴수도 있을까요?
db연동은 그렇다 치고 구글맵도 안나오고있는상황입니다..

댓글을 작성하려면 로그인이 필요합니다.

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

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

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

<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 />
};
이렇게 나옵니다 ㅠㅠ 변수 세개를 다 인식을 못하는것같아요

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고