php-mysqli_fetch_array()期望参数1是mysqli_结果,字符串给定
我正在尝试编写一个PHP代码,用phpmyadmin插入和更新mySQL数据库中的值和行,对现有行的更新可以正常工作,但插入不起作用。为了给您提供一些上下文,deviceID是主键,如果数据库中不存在deviceID,则应该进行插入 我认为问题在于比较if语句是返回空集还是返回结果。感谢您的帮助,提前谢谢 警告:php-mysqli_fetch_array()期望参数1是mysqli_结果,字符串给定,php,mysql,mysqli,Php,Mysql,Mysqli,我正在尝试编写一个PHP代码,用phpmyadmin插入和更新mySQL数据库中的值和行,对现有行的更新可以正常工作,但插入不起作用。为了给您提供一些上下文,deviceID是主键,如果数据库中不存在deviceID,则应该进行插入 我认为问题在于比较if语句是返回空集还是返回结果。感谢您的帮助,提前谢谢 警告: if ($_SERVER ["REQUEST_METHOD"]=="POST"){ require'connectiontest.php'; createStudent(); } $
if ($_SERVER ["REQUEST_METHOD"]=="POST"){
require'connectiontest.php';
createStudent();
}
$response = array();
function createstudent()
{
// check for required fields
if (isset($_POST['deviceId']) && isset($_POST['buildingId']) &&
isset($_POST['levelId']) && isset($_POST['floorplanId']) &&
isset($_POST['latitude']) && isset($_POST['longitude'])
&& isset($_POST['x']) && isset($_POST['y']) && isset($_POST['i']) && i
sset($_POST['j']) && isset($_POST['heading'])
&& isset($_POST['probability']) && isset($_POST['roundtrip'])) {
// extract data from POST into variables
global $connect;
<?php
$deviceId = $_POST['deviceId'];
$buildingId = $_POST['buildingId'];
$levelId = $_POST['levelId'];
$floorplanId = $_POST['floorplanId'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$x = $_POST['x'];
$y = $_POST['y'];
$i = $_POST['i'];
$j = $_POST['j'];
$heading = $_POST['heading'];
$probability = $_POST['probability'];
$roundtrip = $_POST['roundtrip'];
$query = "SELECT * FROM devicelocations WHERE deviceId = '$deviceId';";
$arr = mysqli_fetch_array($query);
if (sizeof($arr) == 1) {
error_log("Database is empty, doing an INSERT.", 0);
$query = "INSERT INTO devicelocations (deviceId, buildingId, levelId, floorplanId, latitude, longitude, x, y, i, j, heading, probability, roundtrip) VALUES ('$deviceId', '$buildingId', '$levelId', '$floorplanId', '$latitude', '$longitude', '$x', '$y', '$i', '$j', '$heading', '$probability', '$roundtrip');";}
else {
error_log("Database already has that deviceId, doing an UPDATE.", 0);
$query = "UPDATE devicelocations SET buildingId = '$buildingId', levelId = '$levelId', floorplanId = '$floorplanId', latitude = '$latitude', longitude = '$longitude', x = '$x', y = '$y', i = '$i', j = '$j', heading = '$heading', probability = '$probability', roundtrip = '$roundtrip' WHERE deviceId = '$deviceId';";}
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
// check if row inserted or not
if ($query) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Entry successfully inserted or updated.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Entry was not successfully inserted or updated.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
}
?>
警告:mysqli_fetch_array()要求参数1为mysqli_结果,
在C:\wamp\www\android\u connect\update.php行中给出的字符串
43
第43行是:$arr=mysqli\u fetch\u数组($query)代码>
以下是php代码:
if ($_SERVER ["REQUEST_METHOD"]=="POST"){
require'connectiontest.php';
createStudent();
}
$response = array();
function createstudent()
{
// check for required fields
if (isset($_POST['deviceId']) && isset($_POST['buildingId']) &&
isset($_POST['levelId']) && isset($_POST['floorplanId']) &&
isset($_POST['latitude']) && isset($_POST['longitude'])
&& isset($_POST['x']) && isset($_POST['y']) && isset($_POST['i']) && i
sset($_POST['j']) && isset($_POST['heading'])
&& isset($_POST['probability']) && isset($_POST['roundtrip'])) {
// extract data from POST into variables
global $connect;
<?php
$deviceId = $_POST['deviceId'];
$buildingId = $_POST['buildingId'];
$levelId = $_POST['levelId'];
$floorplanId = $_POST['floorplanId'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$x = $_POST['x'];
$y = $_POST['y'];
$i = $_POST['i'];
$j = $_POST['j'];
$heading = $_POST['heading'];
$probability = $_POST['probability'];
$roundtrip = $_POST['roundtrip'];
$query = "SELECT * FROM devicelocations WHERE deviceId = '$deviceId';";
$arr = mysqli_fetch_array($query);
if (sizeof($arr) == 1) {
error_log("Database is empty, doing an INSERT.", 0);
$query = "INSERT INTO devicelocations (deviceId, buildingId, levelId, floorplanId, latitude, longitude, x, y, i, j, heading, probability, roundtrip) VALUES ('$deviceId', '$buildingId', '$levelId', '$floorplanId', '$latitude', '$longitude', '$x', '$y', '$i', '$j', '$heading', '$probability', '$roundtrip');";}
else {
error_log("Database already has that deviceId, doing an UPDATE.", 0);
$query = "UPDATE devicelocations SET buildingId = '$buildingId', levelId = '$levelId', floorplanId = '$floorplanId', latitude = '$latitude', longitude = '$longitude', x = '$x', y = '$y', i = '$i', j = '$j', heading = '$heading', probability = '$probability', roundtrip = '$roundtrip' WHERE deviceId = '$deviceId';";}
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
// check if row inserted or not
if ($query) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Entry successfully inserted or updated.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Entry was not successfully inserted or updated.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
}
?>
if($\u服务器[“请求\u方法”]=“发布”){
需要'connectiontest.php';
createStudent();
}
$response=array();
函数createstudent()
{
//检查必填字段
如果(isset($_POST['deviceId'])和isset($_POST['buildingId'])和
isset($_POST['levelId'])和isset($_POST['floorplanId'])和
isset($_POST['latitude'])和&isset($_POST['longitude'])
&&isset($_POST['x'])&isset($_POST['y'])&isset($_POST['i'])&i
sset($_POST['j'])和isset($_POST['heading']))
&&isset($_POST['probability'])&isset($_POST['roundtrip'])){
//将POST中的数据提取到变量中
全球$connect;
使用
在必须建立与数据库的连接之前,您甚至没有执行查询
$query = "SELECT * FROM devicelocations WHERE deviceId = '$deviceId';";
$result=mysqli_query($con,$query);//executing query
$arr = mysqli_fetch_array($result);
$con=mysqli\u连接(ip,“用户名”,“密码”,数据库)
是否连接到数据库这解决了问题:
$result=mysqli_query($connect,$query);
$count=mysqli_num_rows($result);
if($count>0) {
update query } else{
insert query }
与数据库的连接工作正常,我可以用另一个代码更新插入的行中的值。现在我按照您所说的执行了查询,我没有收到任何警告,但仍然无法插入新行。我进行了编辑;“require'connectiontest.php';”是连接完成的地方。插入新行是指“INSERT INTO”语句不起作用。sixe arr返回的结果大小将大于1,因此使用if(mysqli_num_rows==1)
@Dharman这实际上是一个不同的问题。无论多么愚蠢,任何错误消息都应该得到堆栈溢出的答案。