从列数据库表中回显数组的列表

时间:2019-08-28 10:53:49

标签: php arrays echo

我试图回显列userid中所有行的列表,并用逗号分隔,并从最后一个值中删除最后一个逗号

    $sql_userid = "SELECT `userid` FROM `tabe_users`";
        $result_userid = $con->query($sql_userid);
        if ($result_userid->num_rows > 0) {
           while($row_userid = $result_push->fetch_assoc()) {
           $getallids = mysqli_free_result($result_userid) . ', ';
           $listallids = substr($getallids, 0, -2);
      }
    }

echo $listallids;

1 个答案:

答案 0 :(得分:0)

以此更新代码,如果要以回显方式打印,则无法以回显方式打印数组,而必须再次将其编码为json后再使用其解码。

或仅使用 print_r()

    async function test(){
        var query = `INSERT INTO mdl_user (auth, confirmed, username, password, firstname, lastname, email, mnethostid) 
            VALUES ('googleoauth2', 1, '${pseudo}', 'not cached', '${name}', 
                   '${secondname}', '${email}', 1);`;
        var insertOutput = await getMysqlQuery(query);
        var userId = insertOutput.results.insertId;
        var courseRecords = await getMysqlQuery("SELECT id FROM mdl_course WHERE idnumber=\"" + shortname + "\"");  

        if(courseRecords.length < 1)
            throw 'Course not found';
        var courseId = courseRecords.results[0].id;
        var enrolRecords = await getMysqlQuery(`SELECT id FROM mdl_enrol WHERE courseid=${courseId} AND enrol='manual';`);

        if(enrolRecords.length < 1)
            throw 'Enrol not found';
        var enrolId = enrolRecords.results[0].id;
        var contextRecords = await getMysqlQuery(`SELECT id FROM mdl_context WHERE contextlevel=50 AND instanceid=${courseId};`);

        if(contextRecords.length < 1)
            throw 'Context not found';
        var now = (new Date()).getTime() / 1000 ;
        var contextId = contextRecords.results[0].id;

        await getMysqlQuery(`INSERT INTO mdl_user_enrolments (status, enrolid, userid, 
                              timestart, timeend, timecreated, timemodified) VALUES 
       (0, ${enrolId}, ${userId}, '${now}', '${now + 60*60*24*2}', '${now}', '${now}')`);

        await getMysqlQuery(`INSERT INTO mdl_role_assignments 
                                    (roleid, contextid, userid, timemodified) 
                               VALUES (5, ${contextId}, '${userId}', '${now}');`);
    }