Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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
MySQLI查询结果(多行)到PHP中的JSON_Php_Mysql_Json_Associative Array - Fatal编程技术网

MySQLI查询结果(多行)到PHP中的JSON

MySQLI查询结果(多行)到PHP中的JSON,php,mysql,json,associative-array,Php,Mysql,Json,Associative Array,我无法使用JSON_encode()php函数查看作为JSON对象返回的mysql查询结果的所有行。 这是我的密码: $Sql_Query = "SELECT * FROM Users"; $result = mysqli_query($dbc,$Sql_Query); $ligne = array(); $bilan = array(); while ($rowr = mysqli_fetch_assoc($result)) { $ligne = array ( "U

我无法使用JSON_encode()php函数查看作为JSON对象返回的mysql查询结果的所有行。 这是我的密码:

$Sql_Query = "SELECT * FROM Users";
$result = mysqli_query($dbc,$Sql_Query);
$ligne = array();
$bilan = array();

while ($rowr = mysqli_fetch_assoc($result)) {
    $ligne = array (
        "User_ID" => $rowr['User_ID']
    );
    $bilan[$ligne['User']] = $ligne[[
        ['User_ID'][$rowr['User_ID']]
    ]];
    array_push($bilan, $ligne);
}
echo json_encode($bilan, JSON_FORCE_OBJECT);
它返回给我:

“19”号,2“““{”用户界面ID”号,2““{”用户界面ID“78”号”,3“““{”用户界面ID“78”号,”3“““{”用户界面ID“78”号,”3“{”用户界面ID“79”号,”3,“3”号,”0 0 0 0 0 0 0 0,”0 0 0 0 0““““:::{”用户界面ID”号,0 0,”0 0 0 0 0 0 0 0 0 0 0,”0 0 0,”0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0“““““,00 0,”0,”0 0 0 0 0 0 0 0 0 0 0 0 0“““““““““,0”3,”0,”0,”0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0““““““““““““““““,,,,0”0”3”3”0 0 0 0 0 0 0 0 0用户ID:“95”},13:{“用户ID:“96”},14:{“用户ID:“97”},15:{“用户ID”:10月18日,18“““{”用户界面ID““:”18““““{”用户界面ID““:”101”10,”19“““{”用户界面ID“:”101”,19“““““,”19,”19“““,”19,”19““,”,16”16“,16”16“,16”16 16“““,16”16”16““,17”17 17““““,18”18““““““““““““,”用户用户用户界面ID“,”,15”,19,”19,”16,”16“,16,”16,”16,”16“,16“,16“,16“,16“,16“,,,16,”16,”16,”16,”16,”16,”16,”16,”16“,16“,16“,16“,16“,16“,16“,16“““,16“,16“,16“““,16“““,16““““““““““““用户ID:“111”},29:{“用户ID:“112”},30:{“用户ID:“113”},“31”:{“用户ID”:“114”},“32”:{“用户ID”:“115”},“33”:{“用户ID”:“116”}

现在,我试图将json输出中每条记录的其他字段关联起来,但是当将这些字段添加到我的代码中时,就没有更多的输出了

while ($rowr = mysqli_fetch_assoc($result)) {
    $ligne = array (
        "User_ID" => $rowr['User_ID'],
        "User_Nom" => $rowr['User_Nom']
    );      
    $bilan[$ligne['User']] = $ligne[[
        ['User_ID'][$rowr['User_ID']]
    ][
        ['User_Nom'][$rowr['User_Nom']]
    ]];
    array_push($bilan, $ligne);
}
echo json_encode($bilan, JSON_FORCE_OBJECT);
似乎可以显示数字值,而不是字母字符

请帮助我在同一输出中混合数字和字母内容

谢谢
Arnaud未初始化$ligne['User'],请尝试以下操作:

while ($rowr = mysqli_fetch_assoc($result)) {
    $ligne = array (
        "User_ID" => $rowr['User_ID'],
        "User_Nom" => $rowr['User_Nom']
    );      
    array_push($bilan, $ligne);
}
echo json_encode($bilan, JSON_FORCE_OBJECT);
我用这个代码测试了它

$result[] = ['User_ID' => 1, 'User_Nom' => 'Nom1'];
$result[] = ['User_ID' => 2, 'User_Nom' => 'Nom2'];
$result[] = ['User_ID' => 3, 'User_Nom' => 'Nom3'];
$bilan = [];
while ($rowr = array_pop($result)) {
    $ligne = array (
        "User_ID" => $rowr['User_ID'],
        "User_Nom" => $rowr['User_Nom']
    );
    array_push($bilan, $ligne);
}
echo json_encode($bilan, JSON_FORCE_OBJECT);
它提供了以下结果:

{“0”:{“User_ID”:3,“User_Nom”:“Nom3”},“1”:{“User_ID”:2,“User_Nom”:“Nom2”},“2”:{“User_ID”:1,“User_Nom”:“Nom1”}

注意:结果有不同的开始,它不包含

“”:空


更多。这是由于奇怪的
$bilan[undefined]=undefined
为清晰起见转换为PHP>=5.5的结果。

我在猜测和假设,但我还能做什么呢?我看到的主要问题是,数组语法可能会让您大吃一惊。似乎您希望通过“User_ID”重新索引结果,我假设每个表记录中都有一些字符串标识符

模块化,以程序形式

/*
    Assuming you are attempting to re-index by the 'User_ID' field
    of each record before encoding as JSON.
*/

function getDb($ip, $user, $password, $database) {
    $db = mysqli_connect($ip, $user, $password, $database);

    //error checking etc ...

    return $db;
}

function selectRecords(mysqli $db, $sql) {
    $result = mysqli_query($db, $sql);

    if (!$result) {
        throw new UnexpectedValueException("Database query (read) was unsuccessful!");
    }

    return $result;
}

function getUserRecords(mysqli $db) {
    $query = 'SELECT * FROM Users';
    return selectRecords($db, $query);
}

function reindexByField($newIndex, $userResults) {
    $reindexed = [];

    while ($row = mysqli_fetch_assoc($userResults)) {
        if (!isset($row[$newInded])) {
            throw new OutofBoundsException("The index '" . $newIndex . "' does not exist in the tested record");
        }

        $redindexed[$row[$newIndex]] = $row;
    }

    return $reindexed;
}

function getJsonFromArray(array $records) {
    $json = json_encode($records, JSON_FORCE_OBJECT);

    if (!$json) {
        throw new UnexpectedValueException("Records were not encoded into a JSON formatted string. Got boolean false, instead.");
    }

    return $json;
}
以程序的形式,然后

try {
    $db = getDb($ip, $user, $password, $db); // Just pretend for a moment.
    echo getJsonFromArray(reindexByField('User_ID', getUserRecords($db));
} catch (e) {
    // Your handler code here.
} finally {
    mysqli_close($db);
}

面向对象的方法可以使您的代码更有条理。

您是对的,因为最初的干净计划不起作用,所以我做了太多修改,最终在不需要的地方增加了复杂性

我发现在php文档中可以了解更多关于在json转换中添加其他字段时生成的错误的信息。json_last_error()是理解该问题的关键。 所以我补充说:

switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - Aucune erreur';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Profondeur maximale atteinte';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Inadéquation des modes ou underflow';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Erreur lors du contrôle des caractères';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Erreur de syntaxe ; JSON malformé';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Caractères UTF-8 malformés, probablement une erreur d\'encodage';
        break;
        default:
            echo ' - Erreur inconnue';
        break;
    }
这会返回一个UTF-8编码问题。所以我修改了代码,添加了一些

utf8_encode($rowr['Fieldname'])
第一个工作解决方案与@PaulH one的非常接近,只是,在我的具体案例中,我必须添加(utf8_encode())语句:

    $Sql_Query = "SELECT * FROM Users";
    $result = mysqli_query($dbc,$Sql_Query);
    $ligne =array();
    $bilan = array();
    while ($rowr = mysqli_fetch_assoc($result)) {
        $ligne = array ("User_ID" => $rowr['User_ID'],  
                        "User_Nom" => utf8_encode($rowr['User_Nom']),
                        "User_Prenom" =>utf8_encode($rowr['User_Prenom']));
        array_push ($bilan, $ligne);
    }
    echo json_encode($bilan, JSON_FORCE_OBJECT); 
现在它显示了所有的字段,所有的行。但是仍然有一些“é”被转换为“\u00e9”。所以把最后一块砖放到解决方案中

我修改了:

JSON_强制_对象

JSON_UNESCAPED_UNICODE

作为json_encode()参数

最后,我想要的代码如下所示:

$Sql_Query = "SELECT * FROM Users";
$result = mysqli_query($dbc,$Sql_Query);
$bilan = array();
while ($rowr = mysqli_fetch_assoc($result)) {
    $ligne = array ("User_ID" => $rowr['User_ID'],  
                    "User_Nom" => utf8_encode($rowr['User_Nom']),
                    "User_Prenom" =>utf8_encode($rowr['User_Prenom']));
    array_push ($bilan, $ligne);
}
echo json_encode($bilan, JSON_UNESCAPED_UNICODE);

你声称正在工作的代码不可能产生你建议的输出。你能用你的实际代码更新吗?我不理解这段代码
$bilan[$ligne['User']]=…
很奇怪,因为
$ligne['User']
没有设置为值,它没有初始化。