douyi4205 2016-01-26 04:20
浏览 135

php mysql绑定到参数

I am newbee and want to learn about php, got below code from google.

Not understand this line of the code.

What is the meaning of this line of code $where = ($data_id) ? 'WHERE id = :data_id' : '';

is it mean, if $data_id is not empty than set variable $where to WHERE id = (param data_id)?

How about if i want to change it to WHERE id like (param data_id) ?

<?php
    include 'db.php';
    $id = (isset($_GET['uid'])) ? $_GET['uid'] : NULL;

    getById($id);

     function getById($data_id) {
         $where = ($data_id) ? 'WHERE id = :data_id' : '';
         $sql="SELECT * from tbl_user $where ORDER BY id desc";
         try {
             $db = getDB();
             $stmt = $db->prepare($sql);
             $stmt->bindParam("data_id", $data_id);
             $stmt->execute();
             $listed = $stmt->fetchAll(PDO::FETCH_OBJ);
             $db = null;
             echo '{"data": ' . json_encode($listed ) . '}';
         } catch(PDOException $e) {
             echo '{"error":{"text":'. $e->getMessage() .'}}';
         }
     }
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
    • ¥88 实在没有想法,需要个思路
    • ¥15 MATLAB报错输入参数太多
    • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败