单值 Mysqli

2021-12-25 00:00:00 php mysqli

我正在尝试编写一个函数,该函数将使用 mysqli 检查数据库中的单个值,而不必将其放入数组中.除了我已经在这里做的事情,我还能做什么?

I am trying to write a function that will check for a single value in the db using mysqli without having to place it in an array. What else can I do besides what I am already doing here?

function getval($query){
    $mysqli = new mysqli();
    $mysqli->connect(HOST, USER, PASS, DB);
    $result = $mysqli->query($query);
    $value = $mysqli->fetch_array;
    $mysqli->close();
    return $value;
}

推荐答案

怎么样

$name = $mysqli->query("SELECT name FROM contacts WHERE id = 5")->fetch_object()->name; 

相关文章