警告:mysqli_error() 正好期望 1 个参数,给定错误 0 个

2022-08-30 17:34:25

我收到以下错误

警告:mysqli_error() 正好期望 1 个参数,给定 0 个参数

问题在于这行代码:

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

整个代码是

session_start();

require_once "scripts/connect_to_mysql2.php";

//Build Main Navigation menu and gather page data here

$sqlCommand = "SELECT id, linklabel FROM pages ORDER BY pageorder ASC";

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) { 
    $pid = $row["id"];
    $linklabel = $row["linklabel"];

    $menuDisplay .= '<a href="index.php?pid=' . $pid . '">' . $linklabel . '</a><br />';

} 
mysqli_free_result($query); 

包含的文件具有以下行

$myConnection = mysqli_connect("$db_host","$db_username","$db_pass","$db_name") or die ("could not connect to mysql"); with reference to $myConnection, why do I get this error?

答案 1

mysqli_error() 需要您将与数据库的连接作为参数传递。此处的文档提供了一些有用的示例:

http://php.net/manual/en/mysqli.error.php

尝试像这样改变你的问题线,你应该处于良好的状态:

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection)); 

答案 2

mysqli_error函数需要作为参数,这就是为什么你得到警告$myConnection


推荐