在文本框中显示mySQL结果

时间:2019-05-25 19:28:54

标签: php 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); ?>" />

我在做什么错了?

2 个答案:

答案 0 :(得分: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();

    $resultText = '';
    $glue = '</br>' . PHP_EOL;
    foreach($user_email as $email) {
        $resultText .= $email . $glue;
    }

    $link->close();

?>

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

答案 1 :(得分:0)

fetch_assoc返回行。

尝试:

$user_email['enter_the_mysql_field_name_here']