简体   繁体   English

如何阻止我的PHP向PHP垃圾邮件发送error.log通知?

[英]How can I stop my PHP from spamming the error.log with PHP Notice?

I'm currently working on a project where I need to get some data from 3 different tables out of a database and echo them so I can work with the results in jQuery. 我目前正在一个项目中,需要从数据库的3个不同表中获取一些数据并回显它们,以便可以在jQuery中处理结果。 I'm sending a GET-request with 3 variables to my PHP. 我正在向我的PHP发送一个带有3个变量的GET请求。 The first one is used to determine which 'command' needs to be executed, the other 2 are used to determine which table and which row needs to be queried. 第一个用于确定需要执行哪个“命令”,其他两个用于确定需要查询哪个表和哪个行。

This is the code I have so far: 这是我到目前为止的代码:

} elseif($_GET['command']=='getpage') {
    $mypid = $_GET['id'];
    $mytable = $_GET['table'];

    $link = mysqli_connect($dbserver,$userdb,$passdb,$db_typo) or die(mysqli_error($link));

    if($mytable == 'tableName1'){
        $query = 'SELECT * FROM table1 WHERE uid = "'.$mypid.'"'; //I need 6 elements from this table
    } elseif($mytable == 'tableName2'){
        $query = 'SELECT * FROM table2 WHERE uid = "'.$mypid.'"'; //I need 7 elements from this table
    } elseif($mytable =='tableName3'){
        $query = 'SELECT * FROM table3 WHERE uid = "'.$mypid.'"'; //I need 8 elements from this table
    } else {
        echo 'no such table supported for this command';
    }

    $result = mysqli_query($link, $query) or die(mysqli_error($link));

    $pagecontent = array();
    while($row = mysqli_fetch_assoc($result)){
        $pagecontent[] = array(
            'id' => utf8_encode($row['uid']),
            'name' => utf8_encode($row['name']),
            'text1' => utf8_encode($row['text1']), //not every table has this
            'text2' => utf8_encode($row['text2']),
            'img' => utf8_encode($row['image']),
            'parent' => utf8_encode($row['parent']), //not every table has this
            'sub_parent' => utf8_encode($row['sub_parent']), //not every table has this
            'deleted' => utf8_encode($row['deleted'])
        );
    }

    echo '{"content": '.json_encode($pagecontent).'}';
}

I have over 50 pages which I need to get from the database. 我有超过50页需要从数据库中获取。 So when I would let the jQuery function that sends the GET-request run trough I would end up spamming the error.log with 因此,当我让发送GET请求的jQuery函数运行通过时,我最终会向error.log发送垃圾邮件,

PHP Notice: Undefined index: text1 in /var/www/my.php on line 171 PHP注意:未定义的索引:第171行的/var/www/my.php中的text1

which I don't want. 我不要

Is there another way to fix this 'problem' than just putting the query and while-loop inside the if-statement? 除了将查询和while循环放入if语句之外,还有另一种解决此“问题”的方法吗?

添加检查是否存在数组键

'text1' => isset($row['text1']) ? utf8_encode($row['text1']) : '',

I don't know whats the reason of why don't you want an if to check for indices, but you could add another loop inside the while 我不知道什么的,为什么你不想要的,如果检查指标,但你可以添加里面另一个循环的原因while

and add those variables: 并添加这些变量:

while($row = mysqli_fetch_assoc($result)){
    $temp = array();
    foreach($row as $key => $value) {
        $temp[$key] = utf8_encode($value);
    }
    $pagecontent[] = $temp;
    // $pagecontent[] = array_map('utf8_encode', $row);
}

echo json_encode(array('content' => $pagecontent));

This just simply gets all the contents inside your $row , encodes all values then push it inside your container. 这只是简单地获取$row所有内容,对所有值进行编码,然后将其推入容器中。

Sidenote: Don't build the JSON string by hand, create the final array structure, then encode in the end. 旁注:请勿手动构建JSON字符串,创建最终的数组结构,然后最后进行编码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM