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
'map' 테이블이 없거나 'map' 테이블에 'idx', 'lng', 'lat' 칼럼이 없을 가능성이 큽니다.
const myLatLng<?=$data['idx']?> = { lat: <?=$data['lat']?>, lng: <?=$data['lng']?> }; //오류
브라우저 소스보기"에서 위 부분이 어떻게 나타나나요?
db table "map"에
idx, lat,lng
이런 컬럼이 있는걸까요?
while($data = mysqli_fetch_array($result)){
?>
이 부분에서
while($data = mysqli_fetch_array($result)){
print_r($data);
?>
를 해보세요
데이터베이스 연동이 안되는거 같네요. 연동되는지 확인부터 필요합니다.
while($data = mysqli_fetch_array($result)) 도달하기 전에
먼저 print_r($result) 로 먼저 확인한번 해보세요
감사합니다 해결했습니다!
답변을 작성하시기 전에 로그인 해주세요.