启动我的代码时,我的查询失败并出现以下错误:
mysqli_query()期望参数1为mysqli,在
中给出nullmysqli_error()期望参数1为mysqli,
中给出的字符串
<?php
include('mysql_config.php');
function mysqlConnect()
{
global $mysql_hostname, $mysql_username, $mysql_password, $mysql_database;
$link = mysqli_connect($mysql_hostname, $mysql_username, $mysql_password)
or die('Could not connect: ' . mysqli_error());
mysqli_select_db($link,$mysql_database) or die('Could not select database');
return $link;
}
function mysqliClose($link)
{
mysqli_close($link);
}
function sendQuery($query)
{
$result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error("could not query"));
return $result;
}
&GT;
如何正确格式化mysqli_query和mysqli_error函数?
答案 0 :(得分:2)
上面的代码中有两个错误:
$link
global
声明为$mysql_hostname
等。mysqli_error()
它预期mysqli
并且您通过了string
我改变了你的榜样:
<?php
include('mysql_config.php');
// declaring an additional global var.
$link = NULL;
function mysqlConnect()
{
global $link; // using the global $link
global $mysql_hostname, $mysql_username, $mysql_password, $mysql_database;
$link = mysqli_connect($mysql_hostname, $mysql_username, $mysql_password)
or die('Could not connect: ' . mysqli_connect_error());
mysqli_select_db($link,$mysql_database) or die('Could not select database');
return $link;
}
function mysqliClose($link)
{
mysqli_close($link);
}
function sendQuery($query)
{
global $link; // using the global $link
$result = mysqli_query($link, $query) or die('Query failed: '
. mysqli_error($link)); // note $link is the param
return $result;
}