phpmysqli

mysqli_query without link parameter


I want to use mysqli_query without first parameter. Is it possible? if yes then what should be my code without database link

<?php
$cnn = mysqli_connect("localhost","root","");
$db = mysqli_select_db($cnn, "thedb");
mysqli_query($cnn, "SET NAMES 'utf8'");
mysqli_query($cnn, 'SET CHARACTER SET utf8'); 
?>

Solution

  • I solve my problem with this code

    public $_link;
    function connectHost($host, $user, $password)
    {
        $this->_link = mysqli_connect($host, $user, $password);
        if(!$this->_link)
        {
            die("Could not connect to host :(");
            return false;
        }
        return true;
    }
    
    public function connectDatabase($database)
    {
        $db = mysqli_select_db($this->_link, $database);
        if(!$db)
        {
            die("Could not connect to database :(");
            return false;
        }
        else
        {
                    mysqli_query($this->_link, "SET NAMES 'utf8'");
                mysqli_query($this->_link, 'SET CHARACTER SET utf8'); 
            return true;
        }
    }
    
    function execQuery($query)
    {
        $result = mysqli_query($this->_link, $query);
        if(!$result)
        {
             //die("Could not execute the query :(");
            return false;
        }
        else
        {
            return $result;
        }
    }