douwen2158 2018-01-09 11:34
浏览 96
已采纳

MySQL查询既不是真也不是假

I have a code which selects certain rows from the table Operators. The connection is successful but when I run the code, I get neither true nor false. neither fail message nor successful

This is the table the Operators table

Note: Changing the $class to "PushCrew" is intentional.

Besides, what is the better/best way to do this task if I have to get the values in an array?

       <?php

$servername = "localhost";
$username = "root";
$password = "indicadls02";
$database = "segmentor";
$conn = new mysqli($servername, $username, $password, $database);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully <br>";

error_reporting(-1);

class gang {

    private $operators;

    public function fetchOperators() {
        $class = get_class($this);
        echo "$class <br>";
        $class = "PushCrew";
        $que = "select * from Operators where Tag = '$class' ";
        echo "$que <br>";
        if(mysqli_num_rows(mysqli_queri($conn, $que)) > 0) {
            echo "query is true <br>";
            $operators = $conn->query($que);
            print_r($operators);
        }
        else {
            echo "select query failed <br>";
        }
    }

    public function getOperators() {
        $this->operators = $this->fetchOperators();
        if(get_parent_class($this)) {
            echo "parent exists <br>";
            echo "$this->operators OPS <br>";
            $parent = get_parent_class($this);
            $temp = new $parent();
            $operatorsInherited = $temp->getOperators();
            echo "$operatorsInherited  inherited<br>";
            $this->operators += ($operatorsInherited);
        }
        else {
            echo "tree finished <br>";
        }
        return $this->operators;
    }
}

class bang extends gang{
    private $operators;
};

$ob2 = new bang();
$ob2->fetchOperators();
//$arr = $ob2->getOperators();
//print_r($arr);
  • 写回答

2条回答 默认 最新

  • du4822 2018-01-11 05:32
    关注

    I have made some changes and now it works. 1. The database connection was not working when posted outside the class. 2. As mentioned in the answer by @Gjord83, I rectified the error.

    Here is the final version(ignore the additional functions):

    <?php
    
    error_reporting(-1);
    
    $con = mysqli_connect("127.0.0.1","root","indicadls02","segmentor");
    
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    class PushCrew {
    
        private $operators;
    
        public function fetchOperators() {
            echo "inside fetchOperators <br>";
            $con = mysqli_connect("127.0.0.1","root","indicadls02","segmentor");
            if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
            $operators = mysqli_query($con,"SELECT * FROM Operators");
            print_r($operators);
            echo "<br>";
        }
    
        public function getOperators() {
            $this->fetchOperators();
            if(get_parent_class($this)) {
                echo "parent exists <br>";
                $parent = get_parent_class($this);
                $temp = new $parent();
                $operatorsInherited = $temp->getOperators();
                //$operators = array_merge($operatorsInherited);
            }
            else {
                echo "tree finished <br>";
            }
        }
    
        public function convertConditionToJsCode() {
    
        }
        public function getJsCode($partialSegments) {
            if (! count($partialSegments)) {
                return null;
            }
    
    
        }
    }
    
    class ClickEvent extends PushCrew{
        private $operators;
    };
    
    
    $ob2 = new ClickEvent();
    $arr = $ob2->getOperators();
    print_r($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错