douren1928 2014-01-27 15:15
浏览 55
已采纳

SQL语句存储在单独的文件夹中[关闭]

Was just wondering from a security standpoint, is it good to have my SQL statements stored in a separate folder like my "files" folder and call them when I need them? Or is this too cumbersome?

I was thinking maybe storing all the statements in one file but I would appreciate input on this.

An example of what I mean.

require('files\selectriskrating.php'); 

And in the files folder I have a selectdetails.php with

$query="SELECT `Riskrating` FROM `currentdetails` WHERE 1";
  • 写回答

1条回答 默认 最新

  • duanbanfei2875 2014-01-27 15:52
    关注

    This is perfectly safe*. You don't need to keep it outside of your web root as another commenter noted because it the server will evaluate it as php and never echo it to the browser.

    However, it is a very cumbersome way of managing your SQL. You are relying on your include to always be error free, in that it always assigns the (presumably) global variable of $query with the right SQL statement. You are also adding a very confusing layer around the SQL itself that will make it hard for others to try to read your code (or even yourself in the future).

    An improvement would be to save the statements as text files. If what you are trying to get at is separation of concerns, then save just the statements as text files, such that

    SELECT `Riskrating` FROM `currentdetails` WHERE 1;
    

    is located in files/myquery.sql. Then load it with

    $query = file_get_contents('files/myquery.sql');
    

    This way, you are eliminating one potential source of errors and you can automatically loop over all of your queries and check them for correctness (eg, in a unit test). Note that if you do this, you will have to keep your queries outside of the document root because they can be shown in the browser.

    However, a much better approach is to use an ORM such as RedBean or Doctrine. This way, you don't have to mess around with your database layer at all.

    * Assuming that the file contents are actually:

    <?php $query = "sql statement";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据