我想使用没有第一个参数的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');
?>
你必须有连接才能执行查询,否则它会在哪里执行?
为了更好地理解这一点,请查看:http://php.net/manual/en/mysqli.query.php
我用这段代码解决了我的问题
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;
}
}