我正在尝试让php脚本多次运行,以便将几个项目从mysql服务器返回到主页面。 这是代码:
function getServerData(){
var serverData = <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT videoId FROM videoList LIMIT 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo json_encode($row["videoId"]);
$deleteCommand = "DELETE FROM videoList WHERE videoId = '".$row["videoId"]."';";
$conn->query($deleteCommand);
$result = "";
}
} else {
echo "'No results'";
}
$conn->close();
?>;
console.log(serverData);
return serverData;
}
是否可以多次使用此代码,或者我是否需要寻找其他路线?