如何在不明确的列名上生成错误消息?

发布于 2024-12-13 21:46:41 字数 533 浏览 0 评论 0原文

我最近经常使用 CodeIgniter,使用数组方法来查询数据:通常是 result_array()row_array() DB 方法。我注意到有时会在没有错误通知的情况下发生错误(旧代码 - 不是我的 - 我只是错误修复者)。这是典型的不明确列名问题,已在 StackOverflow 中多次发布。

例如: PHP MYSQL:如何解决 JOIN 操作中不明确的列名?

使用 CodeIgniter,不会出现不明确的字段错误消息。像往常一样用字段名称填充数组;是否含糊。有没有办法通过显示或记录错误消息来防止 CodeIgnitier 中发生这种情况?

当出现不明确的字段问题时,是否有人知道如何使用 PHP 记录错误消息(也许使用 CI log_message())?

I'm using CodeIgniter a lot lately, with array methods for querying data: usually result_array() or row_array() DB methods. I noticed a mistake that sometimes happens without error notices (old code - not mine - I'm just the bug fixer). It's the typical ambiguous column name issue that has been posted many times here in StackOverflow.

For example:
PHP & MYSQL: How to resolve ambiguous column names in JOIN operation?

With CodeIgniter, there's no ambiguous field error message. The array is populated as usual with the field names; ambiguous or not. Is there anyway to prevent this from within CodeIgnitier by displaying or logging an error message?

Does anyone have any ideas on how to log an error message (using CI log_message() perhaps) with PHP when ambiguous fields issues arise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2024-12-20 21:46:41

这是可能的。 CI 不会抛出有关不明确列名的 SQL 错误的原因是因为它在选择时附加了表的名称,例如

SELECT `table`.`name`, `table2`.`name` FROM `table`, `table2` ...

您看不到这些列的原因是因为 mysqli_fetch_assoc() 或您使用的任何驱动程序都是 name ,并且显然会被覆盖。以下答案适合那些使用 CI2.x 的人。

以与我对 CodeIgniter:所有 $this->db->query() 方法调用的 SQL 审计?。扩展数据库驱动程序结果类后,您有两个选择。您可以按照您的请求抛出错误,如下所示:

/**
 * Result - associative array
 *
 * Returns the result set as an array
 *
 * @access  private
 * @return  array
 */
function _fetch_assoc()
{
    if (!($row = mysql_fetch_array($this->result_id)))
    {
        return null;
    }

    $return = array();
    $row_count = mysql_num_fields($this->result_id);

    for($i = 0; $i < $row_count; $i++)
    {
        $field = mysql_field_name($this->result_id, $i);

        if( array_key_exists( $field, $return ) ) {
            log_message('Error message about ambiguous columns here');
        } else {
            $return[$field] = $row[$i];
        }
    }

    return $return;

    //Standard CI implementation
    //return mysql_fetch_assoc($this->result_id);
}

或者您可以进一步操作函数的输出来模拟查询行为,方法是将 tablename_ 添加到列名称前面,从而生成一个数组,如下所示

SELECT * FROM `users` LEFT JOIN `companies` ON `users`.`id` = `companies`.`id`
Array (
    [users_id] => 1
    [users_name] => User1
    [companies_id] => 1
    [companies_name] => basdasd
    [companies_share_price] => 10.00
)

要实现此目的,您可以可以简单地将上述函数中的 for 循环修改为以下内容:

for($i = 0; $i < $row_count; $i++)
{
    $table = mysql_field_table($this->result_id, $i);
    $field = mysql_field_name($this->result_id, $i);
    $return["$table_$field"] = $row[$i];
}

This is possible. The reason that CI doesn't throw up SQL errors about ambiguous column names is because it appends the name of the table when selecting so for example

SELECT `table`.`name`, `table2`.`name` FROM `table`, `table2` ...

The reason you then don't get to see these columns is because the array key generated by mysqli_fetch_assoc() or whichever driver you're using is name for both and obviously gets overwritten. The following answer is suitable for those using CI2.x.

Extend the Database driver result class in a similar fashion used in my answer to CodeIgniter: SQL Audit of all $this->db->query() method calls?. Once you've extended the database driver result class you have two options. You can throw an error as you requested as follows:

/**
 * Result - associative array
 *
 * Returns the result set as an array
 *
 * @access  private
 * @return  array
 */
function _fetch_assoc()
{
    if (!($row = mysql_fetch_array($this->result_id)))
    {
        return null;
    }

    $return = array();
    $row_count = mysql_num_fields($this->result_id);

    for($i = 0; $i < $row_count; $i++)
    {
        $field = mysql_field_name($this->result_id, $i);

        if( array_key_exists( $field, $return ) ) {
            log_message('Error message about ambiguous columns here');
        } else {
            $return[$field] = $row[$i];
        }
    }

    return $return;

    //Standard CI implementation
    //return mysql_fetch_assoc($this->result_id);
}

Or alternatively you can further manipulate the output of the function to simulate the query behavior by prepending tablename_ to the column name resulting in an array like

SELECT * FROM `users` LEFT JOIN `companies` ON `users`.`id` = `companies`.`id`
Array (
    [users_id] => 1
    [users_name] => User1
    [companies_id] => 1
    [companies_name] => basdasd
    [companies_share_price] => 10.00
)

To achieve this you can simply modify the for loop in the above function to the following:

for($i = 0; $i < $row_count; $i++)
{
    $table = mysql_field_table($this->result_id, $i);
    $field = mysql_field_name($this->result_id, $i);
    $return["$table_$field"] = $row[$i];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文