samedi 7 octobre 2017

PHP Get Method, does not Enter If Statement

I am not sure why it does not enter the if statement in the API class to actually execute the method. The goal of this method is to simply return a specific row of data, when entering a specific username (this is a users database that holds users info etc). Any help in the right direction is welcomed. This is fairly new stuff to me. Thanks

This is the Connect Class. It is working 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".mmysqli_connect_err();

            }
            return $this->con;

        }

    }

    <?php

This class handles all the functions.

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; 


    }

This is the class that handles all the operations.

This is where the method is called from DbOpertaions. It is included and works correctly with other functions.

<?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);

Aucun commentaire:

Enregistrer un commentaire