【发布时间】:2011-07-10 18:45:18
【问题描述】:
我正在从数据库中选择数据。数据库字段名称与类变量名称完全相同。有没有办法将这些数据存储到类变量中而无需单独指定每个变量?
//gets info about a specified file.
//chosen based on a supplied $fileId
function getFileInfo($fileId)
{
//select info from database
$sql = "SELECT id, companyId, url, name, contentType, fileSize, saved, retrieved
FROM files
WHERE id = $fileId";
$results = $this->mysqli->query($sql);
$results = $results->fetch_object();
//store info into class variables
$this->id = $results->id;
$this->companyId = $results->companyId;
$this->url = $results->url;
$this->name = $results->name;
$this->contentType = $results->contentType;
$this->fileSize = $results->fileSize;
$this->saved = $results->saved;
$this->retrieved = $results->retrieved;
}
【问题讨论】:
-
我隐约记得找到了一个 mysqli 方法可以做到这一点,但我现在找不到。
-
@Brian
mysqli_fetch_object有一个可选的类 arg 但它必须是一个字符串查看代码示例我认为它不会工作