参考-此错误在PHP中意味着什么?

本文翻译自:Reference - What does this error mean in PHP?

What is this? 这是什么?

This is a number of answers about warnings, errors, and notices you might encounter while programming PHP and have no clue how to fix them. 这是有关警告,错误和注意事项的许多答案,这些警告,错误和注意事项在您对PHP进行编程时可能会遇到,并且不知道如何解决它们。 This is also a Community Wiki, so everyone is invited to participate adding to and maintaining this list. 这也是一个社区Wiki,因此邀请所有人参与添加并维护此列表。

Why is this? 为什么是这样?

Questions like "Headers already sent" or "Calling a member of a non-object" pop up frequently on Stack Overflow. 诸如“已发送标题”“呼叫非对象成员”之类的问题经常在堆栈溢出中弹出。 The root cause of those questions is always the same. 这些问题的根本原因总是相同的。 So the answers to those questions typically repeat them and then show the OP which line to change in their particular case. 因此,这些问题的答案通常会重复它们,然后向OP显示在特定情况下应更改的行。 These answers do not add any value to the site because they only apply to the OP's particular code. 这些答案不会为网站增加任何价值,因为它们仅适用于OP的特定代码。 Other users having the same error cannot easily read the solution out of it because they are too localized. 具有相同错误的其他用户无法轻松地从中读取解决方案,因为他们过于本地化。 That is sad because once you understood the root cause, fixing the error is trivial. 令人遗憾的是,一旦您了解了根本原因,就可以轻松地修复错误。 Hence, this list tries to explain the solution in a general way to apply. 因此,此列表试图以一种通用的方式解释该解决方案。

What should I do here? 我该怎么办?

If your question has been marked as a duplicate of this one, please find your error message below and apply the fix to your code. 如果您的问题已被标记为与此问题的重复,请在下面找到您的错误消息,并将修复程序应用于您的代码。 The answers usually contain further links to investigate in case it shouldn't be clear from the general answer alone. 答案通常包含进一步的链接,以进行调查,以免仅凭一般答案无法明确答案。

If you want to contribute, please add your "favorite" error message, warning or notice, one per answer, a short description what it means (even if it is only highlighting terms to their manual page), a possible solution or debugging approach and a listing of existing Q&A that are of value. 如果您想做出贡献,请添加“最喜欢的”错误消息,警告或通知,每个答案一个,简短说明其含义(即使只是在其手册页上突出显示术语),可能的解决方案或调试方法以及现有有价值的问答列表。 Also, feel free to improve any existing answers. 此外,请随时改善任何现有答案。

The List 列表

Also, see: 另请参阅:


#1楼

参考:https://stackoom.com/question/ra3S/参考-此错误在PHP中意味着什么


#2楼

Fatal error: Call to a member function ... on a non-object 致命错误:在非对象上调用成员函数...

Happens with code similar to xyz->method() where xyz is not an object and therefore that method can not be called. 发生类似于xyz->method()代码,其中xyz不是对象,因此无法调用该method

This is a fatal error which will stop the script (forward compatibility notice: It will become a catchable error starting with PHP 7). 这是一个致命错误,它将停止脚本(向前兼容性通知:从PHP 7开始,它将成为可捕获的错误)。

Most often this is a sign that the code has missing checks for error conditions. 大多数情况下,这表明代码缺少对错误条件的检查。 Validate that an object is actually an object before calling its methods. 在调用对象的方法之前,先验证该对象实际上是对象。

A typical example would be 一个典型的例子是

// ... some code using PDO
$statement = $pdo->prepare('invalid query', ...);
$statement->execute(...);

In the example above, the query cannot be prepared and prepare() will assign false to $statement . 在上面的示例中,查询无法准备, prepare()false分配给$statement Trying to call the execute() method will then result in the Fatal Error because false is a "non-object" because the value is a boolean. 然后尝试调用execute()方法将导致致命错误,因为false是“非对象”,因为值是布尔值。

Figure out why your function returned a boolean instead of an object. 弄清楚函数为什么返回布尔值而不是对象。 For example, check the $pdo object for the last error that occurred. 例如,检查$pdo对象以查找最后发生的错误。 Details on how to debug this will depend on how errors are handled for the particular function/object/class in question. 有关如何进行调试的详细信息将取决于所处理的特定功能/对象/类的错误处理方式。

If even the ->prepare is failing then your $pdo database handle object didn't get passed into the current scope . 如果->prepare失败,那么您的$pdo数据库句柄对象也不会传递到当前范围内 Find where it got defined. 查找它的定义位置。 Then pass it as a parameter, store it as property, or share it via the global scope. 然后将其作为参数传递,将其存储为属性或通过全局范围共享。

Another problem may be conditionally creating an object and then trying to call a method outside that conditional block. 另一个问题可能是有条件地创建对象,然后尝试在该条件块之外调用方法。 For example 例如

if ($someCondition) {
    $myObj = new MyObj();
}
// ...
$myObj->someMethod();

By attempting to execute the method outside the conditional block, your object may not be defined. 通过尝试在条件块之外执行该方法,可能未定义您的对象。

Related Questions: 相关问题:


#3楼

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given 警告:mysql_fetch_array()期望参数1为资源,给定布尔值

First and foremost: 首先,最重要的是:

Please, don't use mysql_* functions in new code . 请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. 相反,要了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程


This happens when you try to fetch data from the result of mysql_query but the query failed. 当您尝试从mysql_query的结果中获取数据但查询失败时,会发生这种情况。

This is a warning and won't stop the script, but will make your program wrong. 这是一个警告,不会停止脚本,但是会使您的程序出错。

You need to check the result returned by mysql_query by 您需要通过以下方式检查mysql_query返回的结果

$res = mysql_query($sql);
if (!$res) {
   die(mysql_error());
}
// after checking, do the fetch

Related Questions: 相关问题:

Related Errors: 相关错误:

Other mysql* functions that also expect a MySQL result resource as a parameter will produce the same error for the same reason. 出于同样的原因,也希望将MySQL结果资源用作参数的其他mysql*函数将产生相同的错误。


#4楼

Warning: Cannot modify header information - headers already sent 警告:无法修改标头信息-标头已发送

Happens when your script tries to send an HTTP header to the client but there already was output before, which resulted in headers to be already sent to the client. 当脚本尝试将HTTP标头发送到客户端但之前已经有输出时,会发生此情况,这导致标头已经发送到客户端。

This is an E_WARNING and it will not stop the script. 这是一个E_WARNING ,它不会停止脚本。

A typical example would be a template file like this: 一个典型的示例是这样的模板文件:

<html>
    <?php session_start(); ?>
    <head><title>My Page</title>
</html>
...

The session_start() function will try to send headers with the session cookie to the client. session_start()函数将尝试将带有会话cookie的标头发送到客户端。 But PHP already sent headers when it wrote the <html> element to the output stream. 但是PHP在将<html>元素写入输出流时已经发送了标头。 You'd have to move the session_start() to the top. 您必须将session_start()移到顶部。

You can solve this by going through the lines before the code triggering the Warning and check where it outputs. 您可以通过在代码触发警告之前检查各行并检查其输出位置来解决此问题。 Move any header sending code before that code. 将任何标头发送代码移到该代码之前。

An often overlooked output is new lines after PHP's closing ?> . 一个经常被忽略的输出是PHP关闭?>之后的换行符。 It is considered a standard practice to omit ?> when it is the last thing in the file. 当文件中的最后一件事时,省略?>被认为是一种标准做法。 Likewise, another common cause for this warning is when the opening <?php has an empty space, line, or invisible character before it, causing the web server to send the headers and the whitespace/newline thus when PHP starts parsing won't be able to submit any header. 同样,此警告的另一个常见原因是,开头<?php有空格,行或不可见字符,导致Web服务器发送标头和空白/换行符,因此当PHP开始解析时将不会能够提交任何标题。

If your file has more than one <?php ... ?> code block in it, you should not have any spaces in between them. 如果文件中包含多个<?php ... ?>代码块,则它们之间不应有任何空格。 (Note: You might have multiple blocks if you had code that was automatically constructed) (注意:如果您具有自动构造的代码,则可能有多个块)

Also make sure you don't have any Byte Order Marks in your code, for example when the encoding of the script is UTF-8 with BOM. 还要确保您的代码中没有任何字节顺序标记,例如,当脚本的编码为带有BOM的UTF-8时。

Related Questions: 相关问题:


#5楼

Parse error: syntax error, unexpected T_XXX 解析错误:语法错误,意外的T_XXX

Happens when you have T_XXX token in unexpected place, unbalanced (superfluous) parentheses, use of short tag without activating it in php.ini, and many more. 当您将T_XXX令牌放置在意外的地方,不平衡的(多余的)括号,在php.ini中未使用短标签而不激活它的情况下,就会发生这种情况。

Related Questions: 相关问题:

For further help see: 有关更多帮助,请参见:


#6楼

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 解析错误:语法错误,意外T_PAAMAYIM_NEKUDOTAYIM

The scope resolution operator is also called "Paamayim Nekudotayim" from the Hebrew פעמיים נקודתיים‎. 示波器解析算子也被称为“ Paamayim Nekudotayim”,来自希伯来语פעמייםנקודתיים。 which means "double colon". 意思是“双冒号”。

This error typically happens if you inadvertently put :: in your code. 如果您无意中将::放在代码中,通常会发生此错误。

Related Questions: 相关问题:

Documentation: 说明文件:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值