Mysqli面向对象可以存储一行数据


Mysqli object oriented can store a row of data?

我实际上是用它来存储和知道我的数据库的一些字段:

$conn_2 = dbConnect();
$stmt2 = $conn_2->prepare("SELECT firstname, lastname, type FROM BrokerMaster.users WHERE email = ?");
$stmt2->bind_param("s", $email);
$stmt2->execute();
$stmt2->bind_result($firstname, $lastname, $type);
while ($stmt2->fetch()) {
       printf("%s %s %s'n", $firstname, $lastname, $type);
}

但我想做这样的事情:

$stmt2 = $conn_2->prepare("SELECT * FROM BrokerMaster.users WHERE email = ?");
$stmt2->bind_param("s", $email);
$stmt2->execute();
$stmt2->bind_result($result); //??
while ($row = $stmt2->fetch()) {
       $firstname = $row["firstname"]
}

我找不到面向对象的方法。我发现的问题是$result不是一个mysqli_result类(如果我没有错的话),并且与query()不同,execute()bind_results()不会创建它。(我也无法使用这个答案

我的错误(或误解)是什么? 我该怎么做?

当然

可以,但你会使用fetch_assoc()函数。

所以你在mysqli_result上使用fetch_assoc().在一段时间循环中执行此操作,将继续循环,直到有可用的行。

while ($row = $result->fetch_assoc()) {
    //Use $row["column_field"];
}

编辑碰巧我们无法直接从预准备语句中获取结果对象。

$stmt2->store_result(); 
$result = $stmt2->get_result();

现在,您应该能够在mysqli_result上使用fetch_assoc()