简体   繁体   English

CodeIgniter SQL查询错误处理

[英]CodeIgniter SQL query error handling

At this moment i access my posts like this 目前,我访问这样的帖子

http://localhost/post/174565/

and everything works fine, but when i am trying to access them like this 并且一切正常,但是当我试图像这样访问它们时

http://localhost/post/1745s65/

i am getting error 我遇到错误

Error Number: 1054
Unknown column '1745s65' in 'where clause'
SELECT * FROM posts WHERE id = 1745s65;
Filename: D:\Localhost\Apache\htdocs\code\system\database\DB_driver.php
Line Number: 330

i understand why it is there, but how to handle it? 我知道为什么会有它,但是如何处理呢? For example my users don't need to see SQL query, and i want to show them 404 page instead of this block. 例如,我的用户不需要查看SQL查询,我想向他们显示404页而不是此块。

use execptions: 使用执行:

try{
       //here what you need to do with script
         } catch (Exception $e) {
            var_dump($e->getMessage());
            show_404();
              }

i think you can try also to put : 我认为您也可以尝试:

$database['db_debug'] = FALSE; in config/database.php if you want to remove database errors from being shown config/database.php如果要删除显示的数据库错误

Try this code: 试试这个代码:

public function getPostData($iPostId) {
    $sqlQuery = $this->db->get_where('posts', array('id' => $iPostId));
    $aResult = $sqlQuery->result();

    if (empty($aResult)) {
        show_404();
    }
    else {
        return $aResult[0];
    }
}

You need to validate user input to prevent this issues. 您需要验证用户输入以防止出现此问题。 Anyhow, Codeigniter has two preset environments: development and prodution. 无论如何,Codeigniter具有两个预设环境:开发和生产。 Check your root index.php for more information. 检查您的根index.php以获取更多信息。

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

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