php 警告:mysqli_query() 期望参数 1 是 mysqli,字符串在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26639619/
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
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
提问by lixle
I got the warning:
我收到警告:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in (...) on line 6
警告:mysqli_query() 期望参数 1 为 mysqli,第 6 行 (...) 中给出的字符串
My code is here:
我的代码在这里:
<?php
require_once 'conn.php';
$sql = "SELECT user_id, access_lvl, name FROM cms_users ";
$result = mysqli_query($sql, $conn);
回答by Ray
It's exactly as the error states as you're passing arguments to mysqli_query()
incorrectly. Assuming $conn
is your mysqli connection generated at some point by new mysqli()
it should be:
这与错误状态完全一样,因为您mysqli_query()
错误地传递了参数。假设$conn
您在某个时候生成的 mysqli 连接new mysqli()
应该是:
$result = mysqli_query( $conn,$sql) or trigger_error(mysqli_error($conn)));
The way you were calling it you were passing a string, $sql
as the first argument.
您调用它的方式是传递一个字符串,$sql
作为第一个参数。
回答by Sahil
I am 3 years too late but all you need to do is swap the parameters of mysqli_query()
我已经晚了 3 年,但您需要做的就是交换参数 mysqli_query()
$result = mysqli_query($conn, $sql)
回答by Zakhele Harries
I was having this this problem but after swiping
$result = mysqli_query($sql, $conn)
to $result = mysqli_query( $conn,$sql)
我遇到了这个问题,但在滑动
$result = mysqli_query($sql, $conn)
到$result = mysqli_query( $conn,$sql)
I manage to solve this error
我设法解决了这个错误
回答by AdheneManx
I have the same error, although the $result = mysqli_query($conn, $sql) is the correct way around.
我有同样的错误,虽然 $result = mysqli_query($conn, $sql) 是正确的方法。
I var_dump() the $conn object and it is a set object at the time I run the query, but still returns a 'string given' error.
我 var_dump() $conn 对象,它在我运行查询时是一个设置对象,但仍然返回一个“给出的字符串”错误。
I was accessing the $conn object after being parsed into a function that I was using it with, in the same way I've done throughout the whole project without error.
在被解析为我正在使用它的函数后,我正在访问 $conn 对象,就像我在整个项目中所做的一样,没有错误。
Re-declaring the $conn object inside the function, instead of passing it into the function stopped the errors, although this behaviour doesn't occur anywhere else in my project. This isn't an ideal solution either.
在函数内重新声明 $conn 对象,而不是将其传递到函数中停止了错误,尽管这种行为不会发生在我的项目中的其他任何地方。这也不是一个理想的解决方案。
To note: I'm using a .env for local development, which causes no issues and helps with deployment locally/remotely via .git.
请注意:我正在使用 .env 进行本地开发,这不会导致任何问题,并且有助于通过 .git 进行本地/远程部署。
After many hours, I honestly believe there is a PHP bug here, I'm using 7.3.0, but occurred in 7.2.5 as well as I'm definitely parsing it a db connection object, not a string.
几个小时后,老实说,我相信这里存在一个 PHP 错误,我使用的是 7.3.0,但发生在 7.2.5 中,而且我肯定将其解析为 db 连接对象,而不是字符串。
Just posting this for information purposes, in case anyone else runs into it. Thanks.
仅出于信息目的发布此信息,以防其他人遇到它。谢谢。
PS. Passwords shouldn't be stored in the database in plain text and is a major security concern. If the author hasn't adjusted this yet (I know it's an old post), it's important to read:
附注。密码不应以纯文本形式存储在数据库中,这是一个主要的安全问题。如果作者尚未对此进行调整(我知道这是一篇旧帖子),请务必阅读: