警告:mysqli_error()期望的是1个参数,0给出错误

问题描述:

我下面的教程,教程不回答问题的但笔者 - 但这里是我的查询警告:mysqli_error()期望的是1个参数,0给出错误

我得到以下错误警告: 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? 

感谢

+0

我读过文档,但没有成功。 –

+0

与问题无关的东西:“$ var”是多余的。这只是打开一个字符串,看到$ var,将其值放入字符串中,然后退出字符串。换句话说,你可以使用$ var。像mysqli_connect($ db_host,$ db_username ....) – Corbin

+1

@Corbin,它不一定是多余的。一些内置的函数对它们接受的类型是严格的,''var''会强制一个非字符串变量传递给函数的字符串类型。所以如果'$ var = 0;','“$ var”'是'“0”'。 – eyelidlessness

mysqli_error()需要你作为参数传递到数据库的连接。文档在这里有一些有用的例子:

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

试着改变你的问题行像这样,你应该是在良好的状态:

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

+1为手动链接:d – Talisin

+0

您好,感谢您的答复, 现在是给我这个问题 - “mysqli_error()预计参数1是mysqli的” –

+0

虽然我做了什么,你说,这还是给了我错误,所以我改变了所有mysqli到MySQL,它现在工作完美! –

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

起初,问题是因为您没有为mysqli_error放置任何参数。我可以看到它已经根据这里的帖子解决了。最有可能的是,下一个问题是由包含文件的错误文件路径导致的。

你确定这个代码

$myConnection = mysqli_connect("$db_host","$db_username","$db_pass","$db_name") or die ("could not connect to mysql"); 

是在“脚本”文件夹,你的主代码文件是在同一水平脚本文件夹?