简体   繁体   English

警告:mysqli_query() 期望参数 1 为 mysqli,字符串在

[英]Warning: mysqli_query() expects parameter 1 to be mysqli, string given in

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

It's exactly as the error states as you're passing arguments to mysqli_query() incorrectly.当您错误地将参数传递给mysqli_query()这与错误状态完全相同。 Assuming $conn is your mysqli connection generated at some point by new mysqli() it should be:假设$conn是您在某个时候由new mysqli()生成的 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作为第一个参数。

我已经晚了 3 年,但您需要做的就是交换mysqli_query()的参数

$result = mysqli_query($conn, $sql)

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我设法解决了这个错误

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.在函数内重新声明 $conn 对象,而不是将其传递到函数中停止了错误,尽管这种行为不会发生在我的项目中的其他任何地方。 This isn't an ideal solution either.这也不是一个理想的解决方案。

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:如果作者尚未对此进行调整(我知道这是一篇旧帖子),请务必阅读:

Secure hash and salt for PHP passwords PHP 密码的安全散列和盐

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:mysqli_query()期望参数2为字符串,对象在 - Warning: mysqli_query() expects parameter 2 to be string, object given in 警告:mysqli_query() 期望参数 1 为 mysqli,给出的值为 null? - Warning: mysqli_query() expects parameter 1 to be mysqli, null given in? 警告:mysqli_query()期望参数1为mysqli,在中给出null - Warning: mysqli_query() expects parameter 1 to be mysqli, null given in 警告:mysqli_query() 期望参数 1 是 mysqli,在 - <b>Warning</b>: mysqli_query() expects parameter 1 to be mysqli, null given in <b> 警告:mysqli_query() 期望参数 1 为 mysqli,null 给定 - Warning: mysqli_query() expects parameter 1 to be mysqli, null given in mysqli_query()期望参数1为mysqli,字符串为 - mysqli_query() expects parameter 1 to be mysqli, string given in 另一个mysqli_query()期望参数1为mysqli,给定字符串 - Another mysqli_query() expects parameter 1 to be mysqli, string given 如何解决警告:mysqli_query()期望参数2为字符串 - How to fix Warning: mysqli_query() expects parameter 2 to be string mysqli_query() 期望参数 2 是字符串,object 给出 - mysqli_query() expects parameter 2 to be string, object given in 警告:mysqli_query()期望参数2为字符串,对象在C:\\ wamp \\ www中给出 - Warning: mysqli_query() expects parameter 2 to be string, object given in C:\wamp\www
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM