Home >Backend Development >PHP Tutorial >What should you pay attention to when troubleshooting NOTICE in php?

What should you pay attention to when troubleshooting NOTICE in php?

WBOY
WBOYOriginal
2016-12-05 13:44:101341browse

What should you pay attention to when troubleshooting NOTICE in php?

What should you pay attention to when troubleshooting NOTICE in php?

The call seems to have been successful...but I really don’t know what the problem is with this notice


Modified as follows...function update($link, $data, $table, $where = null) {

<code>foreach ( $data as $key => $val ) {
    $set .= "{$key}='{$val}',";
}
$set = trim ( $set, ',' );
$where = $where == null ? '' : ' WHERE ' . $where;
$query = "UPDATE {$table} SET {$set} {$where}";
$res = mysqli_query ( $link, $query );
if ($res) {
    return mysqli_affected_rows ( $link );
} else {
    return false;
}</code>

}What should you pay attention to when troubleshooting NOTICE in php?

·······

I have also defined this set... There will still be problems = =

Reply content:

What should you pay attention to when troubleshooting NOTICE in php?

What should you pay attention to when troubleshooting NOTICE in php?

The call seems to have been successful...but I really don’t know what the problem is with this notice


Modified as follows...function update($link, $data, $table, $where = null) {

<code>foreach ( $data as $key => $val ) {
    $set .= "{$key}='{$val}',";
}
$set = trim ( $set, ',' );
$where = $where == null ? '' : ' WHERE ' . $where;
$query = "UPDATE {$table} SET {$set} {$where}";
$res = mysqli_query ( $link, $query );
if ($res) {
    return mysqli_affected_rows ( $link );
} else {
    return false;
}</code>

}What should you pay attention to when troubleshooting NOTICE in php?

·······

I have also defined this set... There will still be problems = =

You did not define the set variable in the function scope. The reason for reporting the notice is that this is not a fatal error. It just reminds you that using undefined variables may bring adverse consequences.

After reading your revised question, set is still not defined. To be defined before using it. .= is equivalent to using it, and it must be within the function scope, not the foreach scope.

The $set variable is not defined and needs to be defined first above foreach:

<code>$set = '';</code>

The number of rows displayed in general error reports, you can find the problem from the row above the row number

First define a variable $set

Undefined variableUndefined variable

$set must be defined first. If it is not defined, use ‘.=’ directly. This is wrong usage

  1. Use error_reporting(0)or modify error_reporting = E_ALL & ~E_NOTICE in php.ini

  2. Variable initialization $set = '';foreach($data as $k=>$v){$set.="{$k}={$v},";}

Define $set = '';

before foreach
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn