未捕获的错误:在|中的布尔值上调用成员函数fetch_row()基于套接字连接的PHP

时间:2018-07-25 03:47:16

标签: php mysql sql mysqli

在一个PHP文件中,我总共具有三个功能。我的第一个功能运行正常。第二和第三功能只是表名不同,否则一切都很好。当我运行第二个或第三个函数时,它给我以下错误

Uncaught Error: Call to a member function fetch_row() on boolean in 

最近两个功能的下一行都出现错误。

while($row = $result->fetch_row()) {

我已经从phpmyadmin测试了所有查询,并且查询正常,没有任何问题。

我的完整PHP文件如下所示

<?php
require_once("includes/conf.php");


function getAllNumbers() {
    global $conn;
    $sql = "SELECT number from number_list WHERE server=1";
    $result = $conn->query($sql);
    $data = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($data, $row[0]);
        }
    }
    $response["data"] = $data;
    return $response;
}

function getNewNumbers() {
    global $conn;
    $sql = "SELECT number, id from new_numbers WHERE server = 1";
    $result = $conn->query($sql);
    $newData = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($newData, $row[0]);
        $id = $row[1];
        $delete = "DELETE number from new_numbers WHERE id = $id";
        $result = $conn->query($delete);

        }
    }
    $response["newData"] = $newData;
    return $response;
}

function getDeleteNumbers() {
    global $conn;
    $sql = "SELECT number, id from delete_numbers WHERE server = 1";
    $result = $conn->query($sql);
    $deleteData = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($deleteData, $row[0]);
        $id = $row[1];
        $delete = "DELETE number from delete_numbers WHERE id = $id";
        $result = $conn->query($delete);

        }
    }
    $response["deleteData"] = $deleteData;
    return $response;
}

?>

表number_list

id  int(11) NO  PRI NULL    auto_increment  
name    varchar(50) NO      NULL        
number  varchar(50) NO      NULL        
server  int(10) NO      0       
status  int(1)  NO      -1      
last_act    timestamp   NO      CURRENT_TIMESTAMP       
user_id int(11) NO  MUL NULL        
created_at  timestamp   NO      CURRENT_TIMESTAMP       
disable int(11) NO      0       
notify  int(1)  NO      1       
fcm varchar(500)    NO      NULL

表new_numbers

id  int(11) NO  PRI NULL    auto_increment  
number  varchar(50) NO      NULL        
server  int(10) NO      0   

表delete_numbers

id  int(11) NO  PRI NULL    auto_increment  
number  varchar(50) NO      NULL        
server  int(10) NO      0   

我应该清除我每隔xx分钟从基于套接字连接的PHP文件中调用这些函数的过程,该文件始终从命令行运行24/7。我认为这是由于require_once。让我知道是否有人可以给我提示解决问题的方法。 谢谢

2 个答案:

答案 0 :(得分:0)

该错误表明fetch_row正在布尔化以执行操作。更改您的Sql查询。

 function getAllNumbers() {
    global $conn;
    $sql = "SELECT number from number_list WHERE server='1'";
    $result = $conn->query($sql);
    $data = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($data, $row[0]);
        }
    }
    $response["data"] = $data;
    return $response;
}

function getNewNumbers() {
    global $conn;
    $sql = "SELECT number, id from new_numbers WHERE server = '1'";
    $result = $conn->query($sql);
    $newData = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($newData, $row[0]);
        $id = $row[1];
        $delete = "DELETE number from new_numbers WHERE id = '$id'";
        $result = $conn->query($delete);

        }
    }
    $response["newData"] = $newData;
    return $response;
}

function getDeleteNumbers() {
    global $conn;
    $sql = "SELECT number, id from delete_numbers WHERE server = '1'";
    $result = $conn->query($sql);
    $deleteData = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($deleteData, $row[0]);
        $id = $row[1];
        $delete = "DELETE number from delete_numbers WHERE id = '$id'";
        $result = $conn->query($delete);

        }
    }
    $response["deleteData"] = $deleteData;
    return $response;
}

此外,这种方法是对Sql Injections的公开邀请。您应该使用准备好的语句

答案 1 :(得分:-1)

这是错误

$ result = $ conn-> query($ delete);

改为使用此

<?php
require_once("includes/conf.php");


function getAllNumbers() {
    global $conn;
    $sql = "SELECT number from number_list WHERE server=1";
    $result = $conn->query($sql);
    $data = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($data, $row[0]);
        }
    }
    $response["data"] = $data;
    return $response;
}

function getNewNumbers() {
    global $conn;
    $sql = "SELECT number, id from new_numbers WHERE server = 1";
    $result = $conn->query($sql);
    $newData = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($newData, $row[0]);
        $id = $row[1];
        $delete = "DELETE number from new_numbers WHERE id = $id";
        $deleted = $conn->query($delete);

        }
    }
    $response["newData"] = $newData;
    return $response;
}

function getDeleteNumbers() {
    global $conn;
    $sql = "SELECT number, id from delete_numbers WHERE server = 1";
    $result = $conn->query($sql);
    $deleteData = array();
    if($result) {
        while($row = $result->fetch_row()) {
        array_push($deleteData, $row[0]);
        $id = $row[1];
        $delete = "DELETE number from delete_numbers WHERE id = $id";
        $deleted = $conn->query($delete);

        }
    }
    $response["deleteData"] = $deleteData;
    return $response;
}

?>