PHP获取方法,不输入If语句

时间:2017-10-07 12:08:24

标签: php arrays database if-statement get

我不确定为什么它不会在API类中输入if语句来实际执行该方法。此方法的目标是在输入特定用户名时简单地返回特定的数据行(这是保存用户信息的用户数据库等)。欢迎任何正确方向的帮助。这对我来说是相当新的东西。感谢

这是Connect Class。它正在100%工作

 <?php 
    class DbConnect{
    private $con;

        function __construct(){

        }

        function connect(){
            include_once dirname(__FILE__).'/Constants.php';
            $this->con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

            if(mysqli_connect_errno()){
                echo "Failed to connect with database".mysqli_connect_err();

            }
            return $this->con;

        }

    }

    <?php

该类处理所有函数。 编辑:所以经过更多的测试,我相信错误是在这部分代码中。

class DbOperations{
    private $con;


    function __construct(){
        require_once dirname(__FILE__).'/DbConnect.php';

        $db = new DbConnect();

        $this->con = $db->connect();
    }

    public function getTheSpecificUser($username){
        //$username = $_POST['username'];
        //$username = $_POST['username'];



         $stmt= $this->con-> prepare("SELECT `id`, `username`, `email`, 
        `phonenumber`, `birthdate`, `lastname`, `firstname`, `middlename` 
          FROM `users` WHERE `username` = ?");

        $stmt->bind_param("s",  $username);
        $stmt->bind_result( $id, $username, $email, $phonenumber, 
        $birthdate, $lastname, $firstname, $middlename);
        $stmt->execute();

        //$stmt->bind_result("isssssss", $id, $username, $email, 
        //$phonenumber, $birthdate,
        //$lastname, $firstname, $middlename);

$results = array();

        //$results = $stmt->fetch();

        while($stmt->fetch()){


            $result = array();
            $result['id'] = $id;
            //$result['username'] = $username;
            $result['email'] = $email;
            $result['phonenumber'] = $phonenumber;
            $result['birthdate'] = $birthdate;
            $result['lastname'] = $lastname;
            $result['firstname'] = $firstname;
            $result['middlename'] = $middlename;

            array_push($results, $result);
        }
        return $results; 


    }

这是处理所有操作的类。

这是从DbOpertaions调用方法的地方。它包含在其他功能中并可正常使用。

编辑:我认为错误不在这里,因为如果语句取自if语句,那么它应该触发,但如果添加到$ response则没有任何反应。所以错误必须在DbOperaitions类中。

<?php 

    //getting the dboperation class
    require_once '../includes/DbOperations.php';

    $response = array();


    if(isset($_GET['apicall'])){

        switch($_GET['apicall']){


      case 'getthespecificuser':

            if(isset($_GET['username'])){
                echo "you are past the frist if statement";
                $db = new DbOperations();

                if($db->getTheSpecificUser($_GET['username'])){
                    echo "you are in the second if statement";
                        $response['error'] = false; 
                        $response['id'] = $user['id'];
                        $response['username'] = $user['username'];
                        $response['email'] = $user['email'];
                        $response['phonenumber'] = $user['phonenumber'];
                        $response['lastname'] = $user['lastname'];
                        $response['firstname'] = $user['firstname'];
                        $response['middlename'] = $user['middlename'];



                }else{
                    echo "-->";
                    var_dump($_GET['username']);
                    die();
                    $response['error'] = true;
                    $response['message'] = "something went wrong";

                }


            }else{
                $response['error'] = true;
                $response['message'] = "please enter a valid username";

            }
            break;
     }else{
        //if it is not api call 
        //pushing appropriate values to response array 
        $response['error'] = true; 
        $response['message'] = 'Invalid API Call`enter code here`';
    }

    //displaying the response in json structure 
    echo json_encode($response);

0 个答案:

没有答案