没有链接参数的mysqli_query

时间:2018-07-06 13:59:00

标签: php mysqli

我要使用不带第一个参数的mysqli_query。可能吗?如果是的话,那没有数据库链接的代码应该是什么

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

2 个答案:

答案 0 :(得分:0)

您必须具有连接才能执行查询,否则它将在哪里执行?

要更好地理解这一点,请查看:http://php.net/manual/en/mysqli.query.php

答案 1 :(得分:0)

我用这段代码解决了我的问题

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;
    }
}