如何在不明确的列名上生成错误消息?
我最近经常使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的。 CI 不会抛出有关不明确列名的 SQL 错误的原因是因为它在选择时附加了表的名称,例如
您看不到这些列的原因是因为
mysqli_fetch_assoc()
或您使用的任何驱动程序都是name
,并且显然会被覆盖。以下答案适合那些使用 CI2.x 的人。以与我对 CodeIgniter:所有 $this->db->query() 方法调用的 SQL 审计?。扩展数据库驱动程序结果类后,您有两个选择。您可以按照您的请求抛出错误,如下所示:
或者您可以进一步操作函数的输出来模拟查询行为,方法是将
tablename_
添加到列名称前面,从而生成一个数组,如下所示要实现此目的,您可以可以简单地将上述函数中的
for
循环修改为以下内容: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
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 isname
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:
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 likeTo achieve this you can simply modify the
for
loop in the above function to the following: