Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用数据库中的数据构建多维JSON字符串_Php - Fatal编程技术网

Php 使用数据库中的数据构建多维JSON字符串

Php 使用数据库中的数据构建多维JSON字符串,php,Php,嗯 我有一个函数,需要从数据库中检索数据(其中包含谷歌地图标记的信息),并用该数据构建JSON字符串。。因为我需要一个多维json字符串,所以我认为这对我来说是最好的选择 public function getAll(){ $tables = ['huisartsenwachtposten', 'bioscopen']; $markers = []; for($i=0; $i<count($tables); $i++){ $sql = "SELEC

我有一个函数,需要从数据库中检索数据(其中包含谷歌地图标记的信息),并用该数据构建JSON字符串。。因为我需要一个多维json字符串,所以我认为这对我来说是最好的选择

public function getAll(){
    $tables = ['huisartsenwachtposten', 'bioscopen'];
    $markers = [];

    for($i=0; $i<count($tables); $i++){
        $sql = "SELECT * FROM `:table`";

        $stmt = $this->db->prepare($sql);
        if ($stmt) {
            $stmt->bindParam(':table', str_replace("'", "", $tables[$i]));

            if ($stmt->execute()) {
                while($row = $stmt->fetch() ) {
                    $markers[$tables[$i]][] = $row;
                }
            }
        }   
    }
    return json_encode($markers);  
}
公共函数getAll(){
$tables=['huisartsenwachtposten','bioscopen'];
$markers=[];
对于($i=0;$idb->prepare($sql);
如果($stmt){
$stmt->bindParam(':table',str_replace(“'”,“”,$tables[$i]);
如果($stmt->execute()){
而($row=$stmt->fetch()){
$markers[$tables[$i]][]=$row;
}
}
}   
}
返回json_encode($markers);
}
但是,当我运行此函数时,我的浏览器显示以下错误:“SQLSTATE[42S02]:找不到基表或视图:1146表'GEPensionerAgent.'huisartsenwachtposten'不存在”

有解决方案的人吗?:) 提前谢谢


p、 我在本例中使用了两个以上的表…

如果您使用的是PDO,请查看:

bindParam()只用于列,不用于表。你必须把表格的名字写下来

试试这个(未测试!):

公共函数getAll(){
$tables=array('huisartsenwachtposten','bioscopen');
$markers=array();
对于($i=0;$idb->prepare($sql);
如果($stmt){
如果($stmt->execute()){
而($row=$stmt->fetch()){
$markers[$tables[$i]][]=$row;
}
}
}   
}
返回json_encode($markers);
}

如果您正在使用PDO,请查看:

bindParam()只用于列,不用于表。您必须写下表名

试试这个(未测试!):

公共函数getAll(){
$tables=array('huisartsenwachtposten','bioscopen');
$markers=array();
对于($i=0;$idb->prepare($sql);
如果($stmt){
如果($stmt->execute()){
而($row=$stmt->fetch()){
$markers[$tables[$i]][]=$row;
}
}
}   
}
返回json_encode($markers);
}

您不能参数化sql、数据库名、表名、列名等。只能参数化数据。您不能参数化sql、数据库名、表名、列名等。只能参数化数据。是否有方法获取表名,以便我可以使用$markers[tablename][]=$row?我看到了!谢谢你的帮助。我周一有一个关于这个的考试报告,所以你帮了我很大的忙!:有没有办法得到表名,这样我就可以有$markers[tablename][=$row?我看到了!谢谢你的帮助。我周一有一个关于这个的考试报告,所以你帮了我很大的忙!:D
public function getAll(){
    $tables  = array('huisartsenwachtposten', 'bioscopen');
    $markers = array();

    for($i=0; $i<count($tables); $i++){
        $sql = "SELECT * FROM `".$tables[$i]."`";

        $stmt = $this->db->prepare($sql);
        if ($stmt) {
            if ($stmt->execute()) {
                while($row = $stmt->fetch() ) {
                    $markers[$tables[$i]][] = $row;
                }
            }
        }   
    }
    return json_encode($markers);  
}