duannao1920 2016-04-27 18:22
浏览 53
已采纳

PHP7 / mysql PDO自定义功能

I'm trying to migrate an application written in PHP 5.2, but I'm having trouble creating the correct syntax for custom functions. This works perfectly in the old syntax, but I get a fatal error with this.

Essentially, I'd like to create a function to make it easy to get an email address from a database table that's associated with a unique id. I wrote the code below based on what works in PHP 5.2.

function getemail($id) {
    $email_query = $con->query("SELECT email FROM admin WHERE id='$id'"); 
    $rs = $email_query->fetch(PDO::FETCH_ASSOC);

    return $rs;
}

Then I could use something like below to call this function.

foreach($con->query('SELECT * FROM admin') as $row) {
    echo $row['id'] . ' ' . $row['name'] . ' ' . getemail($row['id']) . '<br>';
}

Any direction to help with this issue is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douxieqiu0651 2016-04-27 19:05
    关注

    Ok, I have a tool for such a transition, which combines the simplicity of old mysql functions with safety of PDO (if used properly) - Simple yet efficient PDO wrapper

    Having set it up, all you need is a code like this:

    function getemail($id) {
        return DB::run("SELECT email FROM admin WHERE id=?", [$id])->fetchColumn(); 
    }
    

    called like this

    echo getemail(1); // admin@example.com
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计