<!doctype html>
<html>
<head>
<title>Test MySql Stored Procedure SQL Code via PHP Results</title>
</head>
<body style='background-color:pink;'>
<?php

 // https://dev.mysql.com/doc/refman/5.7/en/spatial-relation-functions-object-shapes.html

 $mfp = "ST_";
 $moref = "";
 $mone="ourmysql_storedprocedure.php";
 $extrasql="";
 $tbits="";
 $tbitstwo="";


 if (isset($_POST['host'])) {
   $ourhost=htmlspecialchars(urldecode($_POST['host']));
 } else {
   $ourhost="localhost:8889";
 }
 if (isset($_POST['user'])) {
   $ouruser=htmlspecialchars(urldecode($_POST['user']));
 } else {
   $ouruser="root";
 }
 if (isset($_POST['password'])) {
   $ourpassword=htmlspecialchars(urldecode($_POST['password']));
 } else {
   $ourpassword="root";
 }

 if (isset($_POST['moref'])) {
   $moref=htmlspecialchars(urldecode($_POST['moref']));
 } else {
   $moref="";
 }

 $con=mysqli_connect($ourhost, $ouruser, $ourpassword);
 // Check connection
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 mysqli_select_db($con, $_POST['db']);
 if (isset($_POST['spqy'])) {
  if (strlen($_POST['spqy']) > 0) {
   $rspes = stripSlashes($_POST['spqy']);
   echo "<details><summary>Creation of tables ...</summary><p>" . str_replace("\r\n","<br>",$rspes) . "</p></details>";
   $spres = mysqli_query($con, $rspes);
  }
 }
 
 echo "\n<div id=textrasqltwo></div>\n";

 if (isset($_POST['spqy2'])) {
  if (strlen($_POST['spqy2']) > 0) {
   $rspes = stripSlashes($_POST['spqy2']);
   echo "<details><summary>CREATE PROCEDURE GetLMax ...</summary><p>" . str_replace("\r\n","<br>",$rspes) . "</p></details>";
   $spres = mysqli_query($con, $rspes);
  }
 } else {
   $mone="area_polygon_mysql.php";
   $moref="^polyid,mapid,onepolygon";
   $extrasql="SELECT CONCAT('If 1 Polygon1 and Polygon2 cross=',CAST((" . $mfp . "CROSSES((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))), CONCAT('If 1 Polygon1 and Polygon2 are disjoint=',CAST((" . $mfp . "DISJOINT((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))), CONCAT('If 1 Polygon1 and Polygon2 are spatially equal=',CAST((" . $mfp . "EQUALS((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))), CONCAT('If 1 Polygon1 and Polygon2 intersect=',CAST((" . $mfp . "INTERSECTS((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))), CONCAT('If 1 Polygon1 and Polygon2 overlap=',CAST((" . $mfp . "OVERLAPS((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))), CONCAT('If 1 Polygon1 and Polygon2 touch=',CAST((" . $mfp . "TOUCHES((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))), CONCAT('If 1 Polygon1 is within Polygon2=',CAST((" . $mfp . "WITHIN((SELECT p.onepolygon FROM POLYGONS p WHERE p.polyid=1), (SELECT q.onepolygon FROM POLYGONS q WHERE q.polyid=1))) AS CHAR(1))) FROM POLYGONS WHERE polyid=1;";
        
 } 
 if (isset($_POST['spqy3'])) {
  if (strlen($_POST['spqy3']) > 0) {
   $rspes = stripSlashes($_POST['spqy3']);
   echo "<details><summary>CREATE PROCEDURE GetPlaceMax ...</summary><p>" . str_replace("\r\n","<br>",$rspes) . "</p></details>";
   $spres = mysqli_query($con, $rspes);
  }
 }
 if (isset($_POST['spqy4'])) {
  if (strlen($_POST['spqy4']) > 0) {
   $rspes = stripSlashes($_POST['spqy4']);
   echo "<details><summary>CREATE PROCEDURE AddLine ...</summary><p>" . str_replace("\r\n","<br>",$rspes) . "</p></details>";
   $spres = mysqli_query($con, $rspes);
  }
 }
 $res = stripSlashes($_POST['qy']);
   echo "<p>" . str_replace("\r\n","<br>",$res) . "</p></details>";
 $mres = mysqli_query($con, explode(";", $res)[0]);
 if (sizeof(explode(";", $res)) > 1) {
   mysqli_close($con);
 $con=mysqli_connect($ourhost, $ouruser, $ourpassword);
 // Check connection
 if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
   mysqli_select_db($con, $_POST['db']);
   $mres = mysqli_query($con, explode(";", $res)[1]);
   }
?>

<div id=textrasql></div>

Query results <b><?php echo str_replace("\r\n","<br>",$_POST['qy']); ?></b><hr>

<?php
if ($mres == 0):
  echo("<b>Error: " . mysqli_error($con) . "</b>");
elseif (mysqli_num_rows($mres) == 0):
  echo("<b>Query executed successfully</b>");
else:
?>
<table border=1>
<thead>
<tr>
<?php
if (substr(($moref . " "), 0, 1) == "^") {
$afs=explode(",", substr($moref,1));
} else {
$afs=explode(",", "lngfrom,latfrom,lngto,latto,distance" . $moref);
}
for ($ii = 0; $ii < sizeof($afs); $ii++) {   
 echo "<th>" . $afs[$ii] . "</th>";
}
?>
</tr>
</thead>
<tbody>
<?php
for ($ii = 0; $ii < mysqli_num_rows($mres); $ii++) {
 echo("<tr>");
 $r_array = mysqli_fetch_row($mres);
 for ($jj = 0; $jj < mysqli_num_fields($mres); $jj++) {
 echo("<td>" . $r_array[$jj] . "</td>");
 }
 echo("</tr>");
}

if ($extrasql != "") {
 if (1 == 1) {
 $tbits=" <scr" . "ipt type='text/javascript'> document.getElementById('textrasql').innerHTML=\"<br>Query results <b>" . str_replace("\r\n","<br>",$extrasql) . "</b><hr><table border=1><tbody>";
 $tbitstwo="  document.getElementById('textrasqltwo').innerHTML=\"<br>Query results <b>" . str_replace("\r\n","<br>",$extrasql) . "</b><hr><table border=1><tbody>";
 } else {
 echo "</tbody></table><br>Query results <b>" . str_replace("\r\n","<br>",$extrasql) . "</b><hr><table border=1><tbody>";
 }
   mysqli_select_db($con, $_POST['db']);
   $mres = mysqli_query($con, $extrasql);
for ($ii = 0; $ii < mysqli_num_rows($mres); $ii++) {
 if (1 == 1) {
  $tbits.='<tr>';
  $tbitstwo.='<tr>';
 } else {
  echo('<tr>');
 }
 $r_array = mysqli_fetch_row($mres);
 for ($jj = 0; $jj < mysqli_num_fields($mres); $jj++) {
 if (1 == 1) {
  $tbits.="<td>" . $r_array[$jj] . "</td>";
  $tbitstwo.="<td>" . $r_array[$jj] . "</td>";
 } else {
  echo("<td>" . $r_array[$jj] . "</td>");
 }
 }
 if (1 == 1) {
  $tbits.='</tr>';
  $tbitstwo.='</tr>';
 } else {
  echo('</tr>');
 }
}
}

?>
</tbody>
</table>
<?php
endif
//mysqli_close();
?>
<hr><br>
<?php
if ($tbits != "" && $tbitstwo != "") {
  echo "\n " . $tbits . "</table>\"; " . $tbitstwo . "</table>\"; </scr" . "ipt> \n";
}
  echo '<a target=_blank href="./' . $mone . '" title="Back">Back for more</a>';
?>
</body>
</html>

