当前位置:首页 > articles > 正文内容

php-mysqli\u fetch\u array()期望参数1是mysqli\u结果,给定字符串[重复]

Luz4周前 (07-20)articles135

php - mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given [duplicate]

  • Jed 问题:
      This question already has answers here:
      Closed last year.
    • 我试图编写一个PHP代码,用phpmyadmin插入和更新mySQL数据库中的值和行,对现有行的更新可以正常工作,但插入不起作用。为了给您提供一些上下文,deviceID是主键,如果数据库中尚不存在deviceID,则应进行插入。
    • 我认为问题在于比较if语句是返回空集还是返回结果。感谢您的帮助,提前谢谢!
    • 警告:
    • 警告:mysqli\u fetch\u array()期望参数1是mysqli\u result,字符串以C:\wamp\www\android\u connect\update给出。php在线
    • line 43 is:
      $arr = mysqli_fetch_array($query);
    • 这是php代码:
    • if ($_SERVER ["REQUEST_METHOD"]=="POST"){require'connectiontest.php';createStudent();}$response = array();function createstudent(){// check for required fieldsif (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 variablesglobal $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 notif ($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 responseecho json_encode($response);}}?>
  • 回答:
    • jasinth premkumar - vote: 1
      • 在必须建立与数据库的连接之前,您甚至没有执行查询。
      • $query = "SELECT * FROM devicelocations WHERE deviceId = '$deviceId';";$result=mysqli_query($con,$query);//executing query$arr = mysqli_fetch_array($result);

      • $con=mysqli_connect(ip,"username","password",database)
      • 是否连接到数据库
    • Jed - vote: 0
      • 这解决了问题:
      • $result=mysqli_query($connect,$query);$count=mysqli_num_rows($result);if($count>0) {update query } else{insert query }
    • Sachin Tripathi - vote: 0
      • 使用
      • $count=mysqli_num_rows($your_query);  if($count>0){       update query}else{       insert query}

相关文章

使用nodejs和mongodb进行前端搜索

Frontend search with nodejs and mongodbCaleb 问题: 我今天刚开始学习mongodb,我有一段代码可以用来查询我的数据库和搜索地址:“Park Lane 38”。这是代码,这是结果:...

单元测试名称重要吗?

Are unit-test names important?Xian 问题: 如果单元测试名称随着时间的推移而变得过时,如果您认为测试本身是最重要的事情,那么选择明智的测试名称是重要的吗?ie[Test]public void Sh...

应该如何使用kotlin将findViewById传递数据从活动页面写入片段页面

How should write the findViewById pass data to Fragment page from Activity page using kotlinMIN LEE 问题: //这是我的活动页面覆盖...

脚本1 linux中的crear usuarios和demás

Script 1 de crear usuarios y demás en linuxMaría Isabel De la Osa Rocha 问题: Cómo se haría el script del usuario porq...

C++:泛型&gt&gt;如何为一个通用函数定义多个模板解题,该函数充当静态实例创建者

C++:Generics >> How to define several template deklarations for a generic function, which serves as an static inst...

三js如何向FBX模型添加多个没有蒙皮的Mixamo动画?

Three.js how to add multiple Mixamo animations without skin to an FBX model?Anye 问题: I'm trying to create a game whe...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。