Mysqli_Query警告:mysqli_query()预计参数1是mysqli的
我在我的代码,这个错误,我不知道如何解决它 我的代码:Mysqli_Query警告:mysqli_query()预计参数1是mysqli的
<?php
session_start();
include_once"connect_to_mysql.php";
$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "root";
// Place the password for the MySQL database here
$db_pass = "****";
// Place the name for the MySQL database here
$db_name = "mrmagicadam";
// Run the actual connection here
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
$menuDisplay="";
while($row=mysql_fetch_array($query)) {
$pid=$row["id"];
$linklabel=$row["linklabel"];
$menuDisplay='<a href="index.php?pid=' .$pid . '">' .$linklabel. '</a><br/>';
}
mysqli_free_result($query);
?>
,这是错误:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\limitless\connect_to_mysql.php on line 17
你能帮助我请
您需要使用
$myConnection= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");
mysqli
拥有原mysql
扩展了许多改进,因此建议您使用mysqli
。
或者跳到PDO,它提供了许多MySQLi不具备的功能,并且使它更容易避免SQL注入。此外,它可以让你做对象映射(如果你不想使用ORM,但喜欢这个想法)... http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the -pros-and-cons – 2012-04-15 08:56:00
您正在使用不正确的语法。如果您阅读文档mysqli_query(),您会发现它需要两个参数。
mixed mysqli_query (mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ])
mysql $link
通常是指在既定的mysqli连接来查询数据库的资源对象。
因此,有解决这个问题的
使用的mysql_query()两种方式
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysql_query($sqlCommand) or die(mysql_error());
或者mysqli_query();
$myConnection= mysqli_connect("$db_host","$db_username","$db_pass", "mrmagicadam") or die ("could not connect to mysql");
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error()) ;
在第二个示例中,您将混合使用MySQL和MySQLi库。在某些情况下,我听说这种方法有效,但我认为只坚持一个是明智的做法。 – halfer 2014-08-01 21:19:36
提示:使用搜索功能来搜索您正在收到的确切错误消息。我可以保证你以前这个问题已经回答了很多次。 – deceze 2012-04-15 08:50:59