将stmt结果存储在数组内部的数组中

时间:2016-10-07 13:00:08

标签: php mysqli

所以我试图创建多个"结果"数组并用我的陈述的结果填充它们,在" main"阵列。

然而,似乎每次循环传递时结果数组都会被覆盖,而不是创建一个额外的数组。

我的代码:

function retrieve_clients() {
    $coach_id = $_SESSION['user_details']['id'];

    $connection = connection();
    $statement = select($connection, '*', 'relations', 'coach_id', '=');
    $statement->bind_param('i', $coach_id);

    if ($statement->execute()) {
        $statement->bind_result($id, $coach_id, $client_id, $relation_date);

        while ($statement->fetch()) {
            $_SESSION['clients'] = array(
                array($id, $coach_id, $client_id, $relation_date)
            );
       }
    }
$statement->close();
}

我再也找不到了。请不要理我,因为我还不熟悉编程。

4 个答案:

答案 0 :(得分:5)

您需要向客户添加另一个维度,方法是将$content = file_get_contents('text.txt'); $censored = explode("\n", file_get_contents('list.txt')); $content = str_replace($censored, '*****', $content); file_put_contents('text.txt', $content); 添加到变量的末尾。

[]

答案 1 :(得分:2)

我认为其他答案都在正确的轨道上,但是它们会增加一个不必要且可能不合适的维度。尝试使用for (let k in list) { jsxList.push( <View> <Text>{list[k]}</Text> <Switch value={this.state.switches[k]} onValueChange={() => this._changeValue(k)}/> </View> ) } 添加动态维度,但删除覆盖的[]维度之一:

array

答案 2 :(得分:1)

那是因为您正在更换以下行中客户的价值,

$_SESSION['clients'] = array(
                array($id, $coach_id, $client_id, $relation_date)
            );

您需要做的是在循环之前先创建一个客户端数组,然后将每个数组附加到主数组中。

像这样,

$_SESSION['clients']=array();
while ($statement->fetch()) {
            $_SESSION['clients'][] = array(
                array($id, $coach_id, $client_id, $relation_date)
            );

答案 3 :(得分:1)

帮自己一个忙,停止使用mysqli(以及家庭酿造&#34;选择&#34;功能)。

使用PDO,您将拥有一个理智的代码:

function retrieve_clients() {
    $sql = "SELECT * FROM relation WHERE coach_id = ?";
    $statement = connection()->prepare($sql); // here goes your SQL
    $statement->execute([$_SESSION['user_details']['id']]); // here goes a parameter
    $_SESSION['clients'] = $statement->fetchAll(); // and here you have your array
}