将 mySQL 结果显示到文本框

我试图从 mySQL 数据库中选择用户电子邮件,然后在文本框中显示结果。但是,我收到一条错误消息:htmlentities() 期望参数 1 为字符串,给定数组


<?php


    if(!isset($_SESSION)){

        session_start();

    }


    include "config.php";


    $q = "SELECT email FROM users WHERE id ='" . $_SESSION['id'] . "'";

    $result = $sql->query($q);

    $user_email = $result->fetch_assoc();


    $link->close();


?>


    <input class="form" type="text" name="email" value="<?php echo html entities($user_email); ?>" />

我究竟做错了什么?


慕少森
浏览 204回答 2
2回答

暮色呼如

因为你的结果是一个数组而不是一个字符串。你可以做什么:<?php&nbsp; &nbsp; if(!isset($_SESSION)){&nbsp; &nbsp; &nbsp; &nbsp; session_start();&nbsp; &nbsp; }&nbsp; &nbsp; include "config.php";&nbsp; &nbsp; $q = "SELECT email FROM users WHERE id ='" . $_SESSION['id'] . "'";&nbsp; &nbsp; $result = $sql->query($q);&nbsp; &nbsp; $user_email = $result->fetch_assoc();&nbsp; &nbsp; $resultText = '';&nbsp; &nbsp; $glue = '</br>' . PHP_EOL;&nbsp; &nbsp; foreach($user_email as $email) {&nbsp; &nbsp; &nbsp; &nbsp; $resultText .= $email . $glue;&nbsp; &nbsp; }&nbsp; &nbsp; $link->close();?>&nbsp; &nbsp; <input class="form" type="text" name="email" value="<?php echo htmlentities($resultText); ?>" />

慕哥6287543

fetch_assoc&nbsp;返回行。尝试:$user_email['enter_the_mysql_field_name_here']
打开App,查看更多内容
随时随地看视频慕课网APP