首页 > 解决方案 > 像返回类型函数一样动态查询

问题描述

像返回类型函数一样动态查询我们可以在我的编程领域使用像动态返回类型函数这样的动态函数吗?

<html>
<head>
<title>Dynamic Querys</title>
</head>
<body>
<?php 
public function fun($table){
 $query="SELECT * FROM ".$table;
 return $query;

}
echo fun("student");
?>
</body>
</html>

标签: phpjquerydatabase

解决方案


你可以这样做

function insert_data($table,$data){
    $sql ="INSERT INTO ".$table;
    $sql .=" (".implode(",",array_keys($data)).") VALUES";
    $sql .=" ('".implode("','", array_values($data))."')";
    $query=$conn->query($sql);
    if ($query) {
        return true;
    }
    else{
        return false;
    }
    $query->close();
}

调用上层函数将数据插入表中。希望这能解决你的问题

$data=array('name' => "Yasir", 'age' => 25, 'education' => "Master");
$fet=insert_data("student",$data);

推荐阅读