Single Value Mysqli

Refiking picture Refiking · Jul 12, 2012 · Viewed 95k times · Source

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;
}

Answer

Mike picture Mike · Jun 3, 2014

How about

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