duanmu3049 2014-10-03 18:31
浏览 10

准备好的声明选择PHP

I'm trying to make a very abstract PDO select function, but for some reason, I'm getting an empty array returned.

function listAllProducts () {
    return select('products', '', '');
} 

function select( $tableName, $property, $value ){
    switch($tableName)
    {
        case "products":
            $tbl = 'products';
            break;
        case "cart":
            $tbl = 'cart';
            break;
    }
    switch($property){
        case "id":
            $property = 'id';
            break;
        case "name":
            $property = 'name';
            break;
    }

    $db = new PDO('mysql:host=localhost;dbname=ecom;charset=utf8', 'root', 'PASSWORD');
    if( strcmp($value, '') != 0 ){ //if $value is not empty
        $query = "SELECT * FROM $tbl WHERE $property = :value";
        $stmt = $db->prepare($query);
        //bind params
        $stmt->bindValue(':value', $value);

    }
    else{
        $query = "SELECT * FROM $tbl";
        $stmt = $db->prepare($query);
    }
    //execute
    try{
        $stmt->execute();
        $result = $stmt->fetchAll();
        return $result;
    }
    catch(Exception $e){
        return $e;
    }
}

Is there something simple I'm missing?

EDIT: Thanks for the help, but after adding a switch and removing table and columns from my bound params, I'm still getting an empty result set :(

EDIT 2: I've found a somewhat convoluted solution, added it above, but it looks like it's working for now! More testing is necessary.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度