Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
用于JSOUP Android的PHP JSON数组编码_Php_Mysql_Arrays_Json - Fatal编程技术网

用于JSOUP Android的PHP JSON数组编码

用于JSOUP Android的PHP JSON数组编码,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我正试图建立一个目录数据库,当人们扫描条形码时,它将与Android应用程序交互以供查看。应用程序应该询问MySQL数据库并查询与部件相关的各种项目。下面是MySQL部分的代码: <?php if(isset($_POST['submit']) || isset($_POST['catalog'])) { require("connect_to_mysql.php"); header("content-type: application/json"); $tabl

我正试图建立一个目录数据库,当人们扫描条形码时,它将与Android应用程序交互以供查看。应用程序应该询问MySQL数据库并查询与部件相关的各种项目。下面是MySQL部分的代码:

<?php
if(isset($_POST['submit']) || isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

    $tables_result = mysql_query("SHOW TABLES") or die(mysql_error());

    while ($table = mysql_fetch_row($tables_result)) {
        $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
        if (mysql_num_rows($query_results) > 0) {
            $fields = mysql_query("SHOW COLUMNS FROM ".$table[0]);

            while ($res = mysql_fetch_array($fields)) {
                $names[] = $res[0];
            }

            $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());

            while($array = mysql_fetch_array($query_results)){
                $rows["part"] = $comments[0];
                $rows["fields"] = $names;
                for($i = 0; $i < mysql_num_fields($query_results); $i++){
                    $rows[$names[$i]] = $array[$i];
                }
                echo json_encode($rows);
            }
        }
    }
    mysql_free_result($tables_result); 
}
if(!isset($_POST['submit']) || !isset($_POST['catalog'])) {
    echo "
    <form method='post'>
    <input type='text' name='catalog' />
    <input type='submit' name='submit' />
    </form>
    ";
}
?>
我在Android客户端使用JSOUP,它具有返回JSONArray的功能。我在过去使用过的JSONArray看起来像这样,JSON中我遇到问题的部分是field对象:

{"part":"Lospa Tibial Baseplate","fields":{["catalog","size"]},"catalog":"01.10.50B","size":"11"}
在对字段数组进行编码之前,我尝试过直接对数组进行编码,但没有成功,并返回了一堆斜杠和无用的东西:

{"part":"Lospa Tibial Baseplate","fields":"[\"catalog\",\"size\"]","catalog":"01.10.50B","size":"11"}
我应该怎么做?我不想只硬编码两个括号,但如果这是最后一个选项,请告诉我如何做,因为我不能只是$rows[fields]={.$names.};

尝试在结构完成构建后对其进行编码:

if(isset($_POST['submit']) || isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

    $tables_result = mysql_query("SHOW TABLES") or die(mysql_error());

    while ($table = mysql_fetch_row($tables_result)) {
        $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
        if (mysql_num_rows($query_results) > 0) {
            $fields = mysql_query("SHOW COLUMNS FROM ".$table[0]);

            while ($res = mysql_fetch_array($fields)) {
                $names[] = $res[0];
            }

            $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());

            while($array = mysql_fetch_array($query_results)){
                $rows["part"] = $comments[0];
                $rows["fields"] = $names;
                for($i = 0; $i < mysql_num_fields($query_results); $i++){
                    $rows[$names[$i]] = $array[$i];
                }
            }

            // JSON encode at the end
            echo json_encode($rows);
        }
    }
    mysql_free_result($tables_result); 
}
如果字段必须是对象,则应该更像这样

{"fields":{"key":["catalog","size"]},"catalog":"01.10.50B","size":"11"}

因此,最好检查JSOUP解析是否正确设置。

我已经找到了答案。下面是我的代码。我基本上必须创建一个编码JSON对象的数组。感谢Kim指出了正确的方向。我还对函数做了一些更改,但这对函数的JSON部分没有影响

<?php
if(isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

$tables_result = mysql_query("SHOW TABLES") or die(mysql_error());
while ($table = mysql_fetch_row($tables_result)) {
    $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
    if (mysql_num_rows($query_results) > 0) {


        $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());
        $columns_sql = mysql_query("SELECT COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_NAME = '".$table[0]."'") or die(mysql_error());
        $rows["validity"] = true;
        $rows["part"] = $comments[0];

        while ($res = mysql_fetch_array($columns_sql)) {
            $names[] = $res[0];
        }
        $rows["fields_count"] = count($names);

        while($array = mysql_fetch_array($query_results)){

            for($i = 0; $i < mysql_num_fields($query_results); $i++){
                $values[] = array($names[$i] => $array[$i]);
            }
        }
        $rows["fields"] = $values;
        echo json_encode($rows);
        exit();
    }
}
echo '{"validity":false}';
mysql_free_result($tables_result); 
}
?>

字段结构不正确

这就是我使用图书馆的原因!它允许避免硬编码我们的JSON

使用$Json->addContentnew arrayJsonfields,$value;将json的字段设置为等于您的值。应该是这样的:

<?php
include('includes/json.php');

$Json = new json();
if(isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

$tables_result = mysql_query("SHOW TABLES") or die(mysql_error());
while ($table = mysql_fetch_row($tables_result)) {
    $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
    if (mysql_num_rows($query_results) > 0) {

        $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());
        $columns_sql = mysql_query("SELECT COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_NAME = '".$table[0]."'") or die(mysql_error());

        $Json->addContent(new propertyJson('status', '200'));
        $Json->addContent(new propertyJson('validity', true));
        $Json->addContent(new propertyJson('part', $comments[0]));

        while ($res = mysql_fetch_array($columns_sql)) {
            $names[] = $res[0];
        }
        $Json->addContent(new propertyJson('count', count($names))); = 

        while($array = mysql_fetch_array($query_results)){

            for($i = 0; $i < mysql_num_fields($query_results); $i++){
                $values[] = array($names[$i] => $array[$i]);
            }
        }

        $Json->addContent(new arrayJson("fields",$values));
        json_send($Json);
        exit();
    }
}
$Json->addContent(new propertyJson('status', '200'));
$Json->addContent(new propertyJson('message', 'Validity failed'));
json_send($Json);
mysql_free_result($tables_result); 
}
?>

尝试对循环中的每一项进行编码总是一个坏主意,总是先在PHP中构建完整的数组结构,然后在处理完之后,再编码JSOUP确实更喜欢字段:{[catalog,size]}?
<?php
if(isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

$tables_result = mysql_query("SHOW TABLES") or die(mysql_error());
while ($table = mysql_fetch_row($tables_result)) {
    $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
    if (mysql_num_rows($query_results) > 0) {


        $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());
        $columns_sql = mysql_query("SELECT COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_NAME = '".$table[0]."'") or die(mysql_error());
        $rows["validity"] = true;
        $rows["part"] = $comments[0];

        while ($res = mysql_fetch_array($columns_sql)) {
            $names[] = $res[0];
        }
        $rows["fields_count"] = count($names);

        while($array = mysql_fetch_array($query_results)){

            for($i = 0; $i < mysql_num_fields($query_results); $i++){
                $values[] = array($names[$i] => $array[$i]);
            }
        }
        $rows["fields"] = $values;
        echo json_encode($rows);
        exit();
    }
}
echo '{"validity":false}';
mysql_free_result($tables_result); 
}
?>
<?php
include('includes/json.php');

$Json = new json();
if(isset($_POST['catalog'])) {
    require("connect_to_mysql.php");
    header("content-type: application/json");

$tables_result = mysql_query("SHOW TABLES") or die(mysql_error());
while ($table = mysql_fetch_row($tables_result)) {
    $query_results = mysql_query("SELECT * FROM ".$table[0]." WHERE catalog = '".$_POST['catalog']."'") or die(mysql_error());
    if (mysql_num_rows($query_results) > 0) {

        $comments = mysql_fetch_row(mysql_query("SELECT table_comment FROM INFORMATION_SCHEMA.TABLES WHERE table_name='".$table[0]."'")) or die(mysql_error());
        $columns_sql = mysql_query("SELECT COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_NAME = '".$table[0]."'") or die(mysql_error());

        $Json->addContent(new propertyJson('status', '200'));
        $Json->addContent(new propertyJson('validity', true));
        $Json->addContent(new propertyJson('part', $comments[0]));

        while ($res = mysql_fetch_array($columns_sql)) {
            $names[] = $res[0];
        }
        $Json->addContent(new propertyJson('count', count($names))); = 

        while($array = mysql_fetch_array($query_results)){

            for($i = 0; $i < mysql_num_fields($query_results); $i++){
                $values[] = array($names[$i] => $array[$i]);
            }
        }

        $Json->addContent(new arrayJson("fields",$values));
        json_send($Json);
        exit();
    }
}
$Json->addContent(new propertyJson('status', '200'));
$Json->addContent(new propertyJson('message', 'Validity failed'));
json_send($Json);
mysql_free_result($tables_result); 
}
?>