I can't seem to get a response from my query method. I have a database connection from method ping_server and use mysqli as connector. Can anyone help me figure out why I cannot print results from mysqli_fetch_array.

dbclass.php

class db
 {

    private $connection;
    private $selectdb;
    private $lastQuery;
    private $config;

    function __construct($config)
    {
        $this->dbConfig = $config;
    }

    function __destruct()
    {


    }


    public function openConn()
    {


        try
        {
            if($this->dbConfig->connector == "mysqli")
            {
                $this->connection = mysqli_connect($this->dbConfig->hostname, $this->dbConfig->username, $this->dbConfig->password);
                $this->selectdb = mysqli_select_db($this->dbConfig->database,$this->connection);

            }
        }   

        catch(exception $e)
        {
            echo'Caught Exception:', $e->getMessage(), "\n";
        }       

    }

    public function closeConn()
    {
        try 
        {
            if($this->dbConfig->connector == "mysqli")
            {
                mysqli_close($this->connection);
            }
        }
        catch(exception $e)
        {
            echo'Caught Exception:', $e->getMessage(), "\n";
        }






    }

    public function query($query)
    {
        //$query = str_replace("}", "", $query);
        //$query = str_replace("{", $this->dbConfig->prefix, $query);

       try 
       {
           if(empty($this->connection))
           {


                $this->openConn();
                $this->lastQuery = mysqli_query($this->connection, $query); 



                $this->closeConn();
                return $this->lastQuery;
            }
            else 
            {
                echo '2nd';

                $this->lastQuery = mysqli_query($this->connection, $query);
               // $this->closeConn();
                echo $lastQuery;
                return $this->lastQuery;


            }
       }

       catch(exception $e)
       {

        echo'Caught Exception:', $e->getMessage(), "\n";

       }
    }

    #get last query 
    public function lastQuery()

    {

        return $this->lastQuery;

    }

    #check to see if we have a connection
    public function pingServer()
    {

        try 
        {
            if($this->dbConfig->connector == "mysqli")
            {
                if(!mysqli_ping($this->connection))
                {

                    return false;

                }
                else
                {

                    return true;

                }



            }       


        }
        catch(exception $e)

        {
            echo'Caught Exception:', $e->getMessage(), "\n";

        }




    }


    public function fetchArray($result)

    {
        try
        {
            if($this->dbConfig->connector == "mysqli")
            {

                return mysqli_fetch_array($result,MYSQL_ASSOC);

            }
            else
            {
                return mysqli_fetch_array($result,MYSQL_ASSOC);

            }


        }
        catch(exception $e)
        {

            echo'Caught Exception:', $e->getMessage(), "\n";

        }





    }



 }

dbfile.php

<?php 

include ('dbclass.php');

#dbConfig object with database data

$config = new dbConfig("hostname","username","password","table","mysqli");

$db = new db($config);

$db->openConn();

$test = $db->pingServer();

echo "Result of Ping:  " . $test . '<br>'; 
$qry = "select project from PROJECTS_PROJECT where project='ab1234'";

$sql = $db->query($qry);

$db->lastQuery();



$result = $db->fetchArray($sql);

$db->lastQuery();



$db->closeConn();


?>

Hmm add semicolon at the end of the sql querry:

$qry = "select project from PROJECTS_PROJECT where project='ab1234';";

If that don't work hmm enter same sql in phpmyadmin so if it works you have problem with code, secound it can be mysqli problem I think that in older versions of PHP there is a cases when it won't work...

Thanks, I tried the semi-colon and replaced with older mysql connector. Still same problem.

The problem is that mysqli does not throw exceptions, PDO does. Looks like you mixed the two. Check the method result instead. Then you'll probably find out more.

commented: Thanks, your suggestion is correct. I took the result of the method and sure enough found the error. +0

Thanks for the help! Found the issue. As @pritaeus suggested mysqli doesn't catch exceptions. Now Im getting return values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.