[PHP]一个简易的返回JSON的PHP连接MySQL中间件
一个简易的php连接MySQL中间件,帮一个客户写的,最后不用了,就发出来了
实测易语言,C++,JavaScript都能够post成功,只需post一个参数名为sql的sql语句
<?php $MySql_ServerIP = "127.0.0.1"; $MySql_UserName = "root"; $MySql_Password = "root"; $MySql_DBName =""; $MySQL_Conn = new Mysqli($MySql_ServerIP,$MySql_UserName,$MySql_Password,$MySql_DBName); if ($MySQL_Conn->connect_error){ die("连接失败,失败原因: ".$MySQL_Conn->connect_error); } if($_SERVER['REQUEST_METHOD'] == 'POST') { if(empty($_POST["sql"])) { echo "sql错误"; exit(); } else { $sql = $_POST["sql"]; $result = $MySQL_Conn->query($sql); $JsonArr = array(); while($row = $result->fetch_assoc()) { $JsonArr[] = $row; } echo json_encode($JsonArr); $MySQL_Conn->close(); } } else { echo "禁止访问!"; } ?>