Php – Warning: thesqli_error() expects exactly 1 parameter, 0 given error


I get the following error

Warning: mysqli_error() expects exactly 1 parameter, 0 given

The problem is with this line of the code:

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

The whole code is


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 />';


The included file has the following line

$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?

Best Solution

mysqli_error() needs you to pass the connection to the database as a parameter. Documentation here has some helpful examples:

Try altering your problem line like so and you should be in good shape:

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