Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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 sql准备语句重新调整空json数组_Php_Mysql_Json - Fatal编程技术网

Php sql准备语句重新调整空json数组

Php sql准备语句重新调整空json数组,php,mysql,json,Php,Mysql,Json,我有3个单选按钮,当我选择其中一个时,我希望从相应的表中加载下拉列表,因此我声明变量$choice以保存单选按钮的状态,并分配2个新变量$table以设置我将从中获取数据的表名,以及另一个$secolumn以设置每个表的列名 然后使用准备好的语句连接到mysql数据库 <?php include "connect.php";//to connect to the mysql DB $table =''; $secolumn =''; if (isset($_GET[&q

我有3个单选按钮,当我选择其中一个时,我希望从相应的表中加载下拉列表,因此我声明变量$choice以保存单选按钮的状态,并分配2个新变量$table以设置我将从中获取数据的表名,以及另一个$secolumn以设置每个表的列名 然后使用准备好的语句连接到mysql数据库

<?php
include "connect.php";//to connect to the mysql DB
$table ='';
$secolumn ='';
if (isset($_GET["table_cond"])) {
    $choice = $_GET["table_cond"];
    if ($choice == "table_fixe") {
        $table = "cr002t_cours_fixe";
        $secolumn ="CR002_DEV_DIN";
    } elseif ($choice == "table_billets") {
        $table = "cr004t_cours_billet";
        $secolumn = "CR004_DEV_BILLC";
    } elseif ($choice == "table_itqb") {
        $table = "cr005t_cours_intbq_comp";
        $secolumn = "CR005_DEV_COMP";
    } else {
        echo "choose a button";
    }
}

$sql ="SELECT DISTINCT GR027DEVISE, GR027LIB 
        FROM gr027t_devises D, ".$table." T 
        WHERE D.GR027DEVISE = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo"sql statement failed";
}else {
    mysqli_stmt_bind_param($stmt, "s", $secolumn);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
}
$currency_array=[];
while ($row = mysqli_fetch_assoc($result)) {
    $id = $row['GR027DEVISE'];
    $name = $row['GR027LIB'];
    $currency_array[] =array("id"=>$id,"name"=>$name);
}
echo json_encode(['currency_array'=>$currency_array]);
?>
然后在前端,当我点击其中一个单选按钮时,我添加了以下脚本 /下拉列表id选择当前名称的id

<script type="text/javascript">
function checkTABLE(table_cond){
    var sel_curr_name=document.querySelector('#sel_curr_name');
    var content='';
    
    $.ajax({
        type:'get',
        url :'/myfolder/getcurrencylist.php?table_cond='+table_cond,
        success:function(data){
            json=JSON.parse(data);
            console.log(json.currency_array);
            $.each( json.currency_array, function( key, value ) {
                content+='<option value='+value.id+'>'+value.name+'</option>'
                });
                sel_curr_name.innerHTML=content;
        }
    })
}

浏览器控制台中的问题是,当我选择打开按钮时,我得到了一个空数组=>并且在网络选项卡中得到了这个{currency_array:[]}

您是否在phpMyAdmin中运行查询以查看该查询是否返回任何基于该查询的行?您对联接使用了非常旧的语法。您还没有定义联接关系。您应该在定义的关系中使用join TABLE_NAME和on,或者在表使用相同的列名时使用。当直接执行查询时,是否返回结果?@RiggsFolly yeah它工作正常,并获取php MyAdmin中所需的内容即使在实时环境中也要从php中获取错误将这4行添加到任何要调试ini设置“显示错误”的基于MYSQLI的脚本的顶部,1;ini_设置“日志错误”,1;错误报告全部;mysqli_REPORT mysqli_REPORT_ERROR | mysqli_REPORT_STRICT;。这将强制任何MYSQLI_uu错误生成一个异常,您可以在浏览器上看到该异常以及正常的PHP错误。@user3783243我已将查询替换为$sql=SELECT DISTINCT gr027t_udesignes.gr027t_udesignes,gr027t_udesignes.GR027LIB FROM gr027t_udesignes internal JOIN.$table。关于gr027t_装置。GR027装置=?;其中$table是选择收音机时的表格名称,并且?所选表的列名