gpt4 book ai didi

php - 在 php 脚本中执行 sql 查询失败但在 phpmyadmin 中成功

转载 作者:行者123 更新时间:2023-11-30 22:51:01 27 4
gpt4 key购买 nike

我在 php 脚本中有一个插入 sql 查询。查询在 phpmyadmin 中执行,但是当我将它放在 php 脚本中时,它不会执行。我收到的错误是“命令不同步;您现在无法运行此命令”。关于可能有什么问题的任何输入?

以下是php代码:

<?php
$request_method = $_SERVER['REQUEST_METHOD'];
if ($request_method == 'POST') {

//retrieve request body
$requestBody = file_get_contents('php://input');

//write to stats file
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/stats/upload/resourceStats.json', 'a') or die("unable to open file\n");
fwrite($fp, $requestBody, strlen($requestBody));
fclose($fp);

//parsing config file
$monitoring_db_config = parse_ini_file("mySql/monitoringConfig.ini");
//Connect to DB
$mysqli_con = mysqli_connect($monitoring_db_config['host'],$monitoring_db_config['username'],$monitoring_db_config['password']);
if (mysqli_connect_error()) {
die('Connect Error\n');
}

//execute .sql file
$multi_query = file_get_contents("mySql/monitoring.sql");
if(mysqli_multi_query( $mysqli_con , $multi_query) == FALSE ){
echo "multi query failed"."\n";
}

//Insert stats into table
$arr_stat = json_decode($requestBody, true);

$sql_query = "INSERT INTO server_resource_stats (server_id,time,resource,measure,unit) VALUES (1,100,'resource',100,'%');";
if (mysqli_query($mysqli_con,$sql_query) != TRUE) {
echo "error inserting into table\n";
}
//close DB connection
$mysqli->close($mysqli_con);
}
?>

.sql文件如下:

/*Create database 'monitoring'*/
CREATE DATABASE IF NOT EXISTS monitoring;

/*Use database 'monitoring'*/
USE monitoring;

/*Create table 'server_resource_stats'*/
CREATE TABLE IF NOT EXISTS server_resource_stats(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,server_id INT(3) NOT NULL,time INT(12) NOT NULL,resource VARCHAR(20) NOT NULL, measure FLOAT(12,4) NOT NULL, unit VARCHAR(10) NOT NULL);

最佳答案

代码看起来不错,但实际上没有放在我的测试服务器上,请尝试从脚本中打印您的查询,这将检查您是否正确地从文件中获取数据,然后您可以将打印的查询复制并粘贴到 phpmyadmin 中或检查它是否有效.

关于php - 在 php 脚本中执行 sql 查询失败但在 phpmyadmin 中成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153810/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com