从数据库获取数据的最佳方法是什么?

时间:2022-08-08 16:59:46

I try print the value of the database's row in input field, but I failed. The code runs, and nothing error is displayed.

我尝试在输入字段中打印数据库行的值,但是我失败了。代码运行,不显示任何错误。

How can I show the datas in the input values?

如何在输入值中显示数据?

Class file

public function getDatas($field, $id) {
    if ($result = $this->db->query("SELECT $field FROM user WHERE id = $id")) {
        if ($result->num_rows) {
            while ($string = $result->fetch_assoc()){
                               $value = $string[$field];
                            }
            return $value;
        }
        $result->free();
    }
}

html

<?php
  $user = new User;

?>

<input class="form-control" type="text" id="name" name="name" value="<?php echo $user->getDatas('name', $_SESSION['id']);?>" required autocomplete="off">

<?php unset($user);?>

1 个解决方案

#1


0  

In your code, you're retrieving the name but you're not saving it. You need to put it in a variable and then use that variable.

在您的代码中,您正在检索名称,但您没有保存它。您需要将其放在变量中,然后使用该变量。

<?php
  $nameUser = new User;
  $name = $nameUser->getDatas('name', $_SESSION['id']);
?>

Then, in your HTML, use $name.

然后,在您的HTML中,使用$ name。

#1


0  

In your code, you're retrieving the name but you're not saving it. You need to put it in a variable and then use that variable.

在您的代码中,您正在检索名称,但您没有保存它。您需要将其放在变量中,然后使用该变量。

<?php
  $nameUser = new User;
  $name = $nameUser->getDatas('name', $_SESSION['id']);
?>

Then, in your HTML, use $name.

然后,在您的HTML中,使用$ name。