php mysqli_Query 警告:mysqli_query() 期望参数 1 为 mysqli

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10160664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:32:35  来源:igfitidea点击:

Mysqli_Query warning: mysqli_query() expects parameter 1 to be mysqli

phpmysql

提问by shaymaa

I got this error in my code and I don't know how to solve it my code:

我的代码中出现此错误,我不知道如何解决我的代码:

<?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);

?>

and this is error:

这是错误:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\limitless\connect_to_mysql.php on line 17

警告:mysqli_query() 期望参数 1 为 mysqli,资源在 C:\xampp\htdocs\limitless\connect_to_mysql.php 第 17 行

What I am doing wrong?

我做错了什么?

回答by F21

You are mixing mysqliand mysqlextensions, which will not work.

您正在混合mysqlimysql扩展,这将不起作用。

You need to use

你需要使用

$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");   

mysqlihas many improvements over the original mysqlextension, so it is recommended that you use mysqli.

mysqli对原始mysql扩展名有很多改进,所以建议您使用mysqli.

回答by Starx

You are using improper syntax. If you read the docs mysqli_query()you will find that it needs two parameter.

您使用的语法不正确。如果您阅读文档mysqli_query()您会发现它需要两个参数。

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

混合 mysqli_query ( mysqli $link , 字符串 $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

mysql $linkgenerally means, the resource object of the established mysqli connection to query the database.

mysql $link一般是指,建立mysqli连接的资源对象来查询数据库。

So there are two ways of solving this problem

所以有两种方法可以解决这个问题

mysqli_query();

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(mysqli_error($myConnection));

Or, Using mysql_query()(This is now obselete)

或者,使用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());

As pointed out in the comments, be aware of using die to just get the error. It might inadvertently give the viewer some sensitive information .

正如评论中指出的那样,请注意使用 die 来获取错误。它可能会在不经意间给观众一些敏感信息。