PDO - passing a field name as a variable

后端 未结 2 1539
粉色の甜心
粉色の甜心 2021-01-13 17:39

I\'m just migrating my code from mysql_query style commands to PDO style and I ran into a problem. THe old code looked like this :

$query_list_menu = \"SELE         


        
2条回答
  •  花落未央
    2021-01-13 18:29

    There is no good and safe way to select just one field from the record based on the user's choice. The most sensible solution would be to select the whole row and then return the only field requested

    $sql = "SELECT * from myl_menu_hide_show WHERE id=?";
    $stmt = $db->prepare($query_list_menu);
    $stmt->execute([$_GET['id']]);
    $row = $stmt->fetch();
    return $row[$_GET['section_name']] ?? false;
    

提交回复
热议问题