{"id":27631,"date":"2022-12-25T13:29:20","date_gmt":"2022-12-25T07:59:20","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/"},"modified":"2022-12-25T13:29:20","modified_gmt":"2022-12-25T07:59:20","slug":"solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/","title":{"rendered":"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-68325277\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"68325277\" data-parentid=\"68325096\" data-score=\"-1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The following is wholly untested! The <code>Object Orientated<\/code> style of using <code>mySQLi<\/code> is considerably less verbose than the <code>procedural<\/code> style and alas your code is vulnerable to SQL injection due to the use of POST data directly used within the SQL statement. You should consider using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.php.net\/manual\/en\/mysqli.quickstart.prepared-statements.php\">Prepared Statements<\/a> to mitigate this threat.<\/p>\n<p>It appears that you could simplify the PHP code to produce some JSON data which you can consume within the Javascript code once the page has loaded. The data is returned following a POST request so presumably the form is on another page not detailed here. You could easily have the form on the same page and remove the need to use a redirection in Javascript &#8211; or even AJAX for a much cleaner effect with no page reloads\/views ( which will increase the map loads recorded by Google )<\/p>\n<p>The recordset is converted to JSON and this is written to the page as a Javascript variable. Once the map is loaded you iterate through this JSON data and add a new marker for each record in the results.<\/p>\n<pre><code>&lt;?php\n    \n    $json=false;\n    \n    if( isset( $_POST['search'] ) ){\n    \n        $term='%'.$_POST['term'].'%';\n        $query='SELECT * from hospitals WHERE hname LIKE ?';\n        \n        $stmt=$db-&gt;prepare( $query );\n        $stmt-&gt;bind_param('s',$term);\n        $stmt-&gt;execute();\n        \n        $res=$stmt-&gt;get_result();\n        $data=$res-&gt;fetch_all( MYSQLI_ASSOC );\n        $json=json_encode( $data );\n    }\n\n?&gt;\n\n&lt;!DOCTYPE html&gt;\n&lt;html lang='en'&gt;\n    &lt;head&gt;\n        &lt;meta charset=\"utf-8\"&gt;\n        &lt;title&gt;LabSeek&lt;\/title&gt;\n\n        &lt;link rel=\"canonical\" href=\"https:\/\/getbootstrap.com\/docs\/5.0\/examples\/sidebars\/\"&gt;\n        &lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"map\/map.css\" \/&gt;\n        &lt;link rel=\"stylesheet\" href=\"styles\/sidebars.css\" \/&gt;\n        &lt;script src=\"https:\/\/polyfill.io\/v3\/polyfill.min.js?features=default\"&gt;&lt;\/script&gt;\n        &lt;script&gt;\n            &lt;?php\n            \n                #Convert the PHP variable into Javascript variable\n                printf('const json=%s;',$json ?: '[]');\n                \n            ?&gt;\n            function initMap() {\n                var options = {\n                    zoom:14,\n                    center: { lat: 14.586225342331847, lng: 120.99824317301842 }\n                }\n                var map = new google.maps.Map( document.getElementById('map'), options );\n                \n                function addMarker( coords, args ){\n                    \/\/ using `Object.assign` allows you to easily \n                    \/\/ add more properties when you call this function\n                    let opts=Object.assign({\n                        position:coords,\n                        map:map             \n                    },args);\n                \n                    return new google.maps.Marker(opts);\n                }\n            \n                if( json &amp;&amp; Object.keys( json ).length &gt; 0 ){\n                    Object.keys( json ).forEach( key=&gt;{\n                        let obj=json[key];\n                        let latlng=new google.maps.LatLng( obj.lat, obj.lng );\n                        let args={\n                            'Address':obj.Address,\n                            'Name':obj.hname\n                        };\n                        \n                        addMarker(latlng,args)\n                    })\n                }\n            }\n        &lt;\/script&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;div id='map'&gt;&lt;\/div&gt;\n        &lt;script async defer src=\"https:\/\/maps.googleapis.com\/maps\/api\/js?key=AIzaSyBQbnrTvBW0ML5bVlgsgMhYCqdRR04d7k0&amp;callback=initMap\"&gt;&lt;\/script&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How do you show multiple markers in Google Maps using PHP from the database when searching? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The following is wholly untested! The Object Orientated style of using mySQLi is considerably less verbose than the procedural style and alas your code is vulnerable to SQL injection due to the use of POST data directly used within the SQL statement. You should consider using Prepared Statements to mitigate this threat. It appears &#8230; <a title=\"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\" aria-label=\"More on [Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[361,492,333,1117,339],"class_list":["post-27631","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-google-maps","tag-javascript","tag-mysqli","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The following is wholly untested! The Object Orientated style of using mySQLi is considerably less verbose than the procedural style and alas your code is vulnerable to SQL injection due to the use of POST data directly used within the SQL statement. You should consider using Prepared Statements to mitigate this threat. It appears ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-25T07:59:20+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?\",\"datePublished\":\"2022-12-25T07:59:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\"},\"wordCount\":212,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"arrays\",\"google-maps\",\"javascript\",\"mysqli\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\",\"name\":\"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-25T07:59:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching? - JassWeb","og_description":"[ad_1] The following is wholly untested! The Object Orientated style of using mySQLi is considerably less verbose than the procedural style and alas your code is vulnerable to SQL injection due to the use of POST data directly used within the SQL statement. You should consider using Prepared Statements to mitigate this threat. It appears ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/","og_site_name":"JassWeb","article_published_time":"2022-12-25T07:59:20+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?","datePublished":"2022-12-25T07:59:20+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/"},"wordCount":212,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","google-maps","javascript","mysqli","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/","url":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/","name":"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-25T07:59:20+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-you-show-multiple-markers-in-google-maps-using-php-from-the-database-when-searching\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How do you show multiple markers in Google Maps using PHP from the database when searching?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=27631"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27631\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=27631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=27631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=27631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}