次のコードは、実行時にエラーを生成しませんが、機能しません。
サーバーコードセクションのクエリをプレースホルダーに置き換えると、echo "8";
問題なく返され、下部のhtmlファイルに希望どおりに表示されます。ただし、PHP の実行は$mysqli->query()
、最初にリストされたファイルの で停止します。MySQL ワークベンチでクエリを実行してみましたが、正常に動作します。PHP でエラーが発生することはありません。困惑...
これが私のサーバーコードです。(db_connect.php)
<?php
error_reporting(E_ALL);
function dbConnect() {
$mysqli = new mysqli("localhost", "root", "", "my_db");
if ($mysqli->connect_errno) {
die ("ERROR: There was an error connecting to the db: " . $mysqli->connect_error);
}
return $mysqli;
}
function getHealth() {
$mysqli = dbConnect();
if($mysqli->query("SELECT * FROM player where name='testname';")) {
printf("This will return the player health here.");
}
else {
printf("query failure: %s", $mysqli->error));
}
$mysqli->close();
}
?>
ここに私の呼び出しコードがあります: (getdata.php)
<?php
error_reporting(E_ALL);
$requestType = $_GET['req']; // TODO: NOT SECURE
require "db_connect.php";
switch($requestType) {
case "health":
echo getHealth();
break;
default:
die ("request not recognized");
}
?>
ここで ajax リクエストが作成されます: ( ajaxexample.html
)
<html>
<head>
<script>
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.write("User Health is: " + xmlhttp.responseText);
}
}
function retrieveUserInfo(requestString) {
xmlhttp.open("GET","getdata.php?req=" + requestString, true);
xmlhttp.send();
}
retrieveUserInfo('health');
</script>
</head>
</body>
</html>