php mysqli_error() 期望参数 1 为 mysqli 是什么意思,null 给定是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7457191/
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-26 02:43:00  来源:igfitidea点击:

What does the mysqli_error() expects parameter 1 to be mysqli, null given mean?

phpmysqlmysqli

提问by chromedude

I have this PHP page:

我有这个 PHP 页面:

<?php

//$_GET['invite'] = kNdqyJTjcf;

$code = mysqli_real_escape_string ($dbc, $_GET['invite']);

$q = "SELECT invite_id FROM signups_invited WHERE (code = '$code') LIMIT 1";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_num_rows($r) == 1) {
    echo 'Verified';
} else {
    echo 'That is not valid. Sorry.';
}

?>

This is returning the error Warning: mysqli_error() expects parameter 1 to be mysqli, null given.

这是返回错误Warning: mysqli_error() expects parameter 1 to be mysqli, null given

Any idea why?

知道为什么吗?

回答by Book Of Zeus

You need to define: $dbcbefore

您需要定义:$dbc之前

 $code = mysqli_real_escape_string ($dbc, $_GET['invite']);

ex:

前任:

$dbc = mysqli_connect("localhost", "my_user", "my_password", "world");

回答by Ken White

Book of Zeusalready gave you the answer. This is for your future reference (and hopefully to help others in the future).

Book of Zeus已经给你答案了。这是供您将来参考(并希望将来可以帮助其他人)。

It's helpful to learn to read error messages and try to figure out what might be causing them. :)

学习阅读错误消息并尝试找出可能导致错误消息的原因很有帮助。:)

This one tells you exactlywhat the problem is, and where to start looking.

这会告诉您确切的问题是什么,以及从哪里开始寻找。

Warning: mysqli_error() expects parameter 1 to be mysqli, null given.

The message tells you that the problem is parameter 1provided to mysqli_error, and that it was nullwhen mysqli_errorexpected it to be a mysqli.

该消息告诉您问题已parameter 1提供给mysqli_error,并且nullmysqli_error预期它是mysqli.

So look at the first parameter you're providing to mysqli_error, and you'll see it's $dbc. You know now that the problem is that $dbcis null when the call to mysqli_error()is made. So look at how it is that $dbccan be null. Oh, right - you didn't declare it and assign anything to it, because the first place it's used in the code is here:

所以看看你提供给 的第一个参数mysqli_error,你会看到它是$dbc. 您现在知道问题是$dbc在调用 时为空mysqli_error()。所以看它是如何是$dbc可以null。哦,对了 - 您没有声明它并为其分配任何内容,因为它在代码中使用的第一个位置在这里:

$code = mysqli_real_escape_string ($dbc, $_GET['invite']);

and it's being used as if it's already something other than null. Since this is at the start of your code, the problem is that you forgot to declare and initialize it by connecting to the database. Problem solved. :)

并且它被用作好像它已经不是null. 由于这是在代码的开头,问题是您忘记通过连接到数据库来声明和初始化它。问题解决了。:)

回答by Arif H-Shigri

undefined variable $dbcin mysqli_error($dbc). you need to put your connection obj here.

未定义的变量$dbcmysqli_error($dbc)。你需要把你的连接对象放在这里。

回答by Sakthi Karthik

Hi please don't confuse with mysql_connect();with mysqli_connect();

嗨,请不要mysql_connect();mysqli_connect();

Try to change as follows

尝试更改如下

$conn=mysqli_connect("yourhost", "username", "password", "database-name") or die("initial host/db connection problem");`

Then if its connected then pass your query with

然后,如果它已连接,则通过您的查询

mysqli_query($conn, "your query string") or die ("query not executed");`

Now it works fine! (mods, removed a to quer.r.y)

现在它工作正常!(mods, 删除 a 到 quer.ry)