doucuo1642 2013-12-24 15:08
浏览 47
已采纳

在PHP中为PDO查询创建容器函数

Because I find PDO executions extremely hard to remember and find myself looking back at previous projects or other websites just to remember how to select rows from a database, I decided that I would try and create my own functions that contain the PDO executions and just plug in the data I need. It seemed a lot simpler than it actually is though...

So far I have already created a connect function successfully, but now when it comes to create a select function I'm stumped for multiple reasons.
For starters there could be a variating amount of args that can be passed into the function and secondly I can't figure out what I should pass to the function and in which order.

So far the function looks like this. To keep me sane, I've added the "id" part to it so I can see what exactly I need to accomplish in the final outcome, and will be replaced by variables accordingly when I work out how to do it.

function sql_select($conn, **what to put here**) {
    try {
        $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
        $stmt->execute(array('id' => $id));

        $result = $stmt->fetchAll();

        if ( count($result) ) {
            foreach($result as $row) {
                print_r($row);
            }
        } else {
            return "No rows returned.";
        }
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}

So far what I've established that the function will need to do is

  • Connect to the database (using another function to generate the $conn variable, already done)
  • Select the table
  • Specify the column
  • Supply the input to match
  • Allow for possible args such as ORDER by 'id' DESC

Lastly from this I would need to create a function to insert, update and delete rows from the database.

Or, is there a better way to do this rather than functions?

If anyone could help me accomplish my ambitions to simply simplify PDO executions it would be greatly appreciated. Thanks in advance!

  • 写回答

4条回答 默认 最新

  • drvvvuyia15070493 2013-12-24 17:14
    关注

    First of all, I have no idea where did you get 10 lines

    $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = ?');
    $stmt->execute(array($id));
    $result = $stmt->fetchAll();
    

    is ALL the code you need, and it's actually three lines, which results with a regular PHP array that you can use wherever you wish. Without the need of any PDO code. Without the need of old mysql code.

    Lastly from this I would need to create a function to insert, update and delete rows from the database.

    DON'T ever do it.

    Please read my explanations here and here based on perfect examples of what you'll end up if continue this way.

    accomplish my ambitions to simply simplify PDO executions

    That's indeed a great ambition. However, only few succeeded in a real real simplification, but most resulted with actually more complex code. For starter you can try code from the first linked answer. Having a class consists of several such functions will indeed improve your experience with PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥60 SOL语句中Where查询中的 from to 语句能不能从小到大换成从大到小(标签-SQL)
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))