这是一个有点棘手的问题,可能看起来很长,但它都是一小段代码和一个相当"简单"的问题。真的需要这方面的帮助(我不允许使用PDO或任何其他数据库处理程序方法,而不是这个,由学校定义):
我有这个类(只有相关字段的缩写)
class Publication {
public $num_author;
public $author;
public function __construct(..., $set_num_author) {
$this->num_author = $set_num_author;
}
public function setAuthor($set_author) {
$this->author = $set_author;
}
public function getNumAuthor() {
return $this->num_author;
}
}
我试图设置它的Author字段。为此,我需要在查询中使用numAuthor值(作者的名称和编号在不同的表中),即:
"SELECT title, abstract, body, DATE_FORMAT(date, '%d-%m-%Y'), type, updated_user_id FROM publication WHERE type = 0 and enabled = 1 ORDER BY created_at DESC";
//gets me the author id
"SELECT name FROM person WHERE id = ?";
//...which I then use to get the author's name
我以出版物为例:
$stmt_pubs_medicos->bind_result($title, $abstract, $body, $date, $type, $num_author);
while ($stmt_pubs_medicos->fetch()) {
$pub = new model'Publication($title, $abstract, $body, $date, $type, $num_author);
array_push($latest_publications, $pub);
}
}
我将它们保存到latest_publications数组中,因为我希望首先只获得3个(只显示第一个,我展开了循环):
$stmt_author->bind_param('i', $pub->getNumAuthor());
$stmt_author->bind_result($author_name);
$stmt_author->fetch();
$latest_publications[0]->setAuthor($author_name);
但是有一个问题。一旦我在网页的div中显示出版物,虽然每个出版物的主体和日期不同,但作者是相同的。即第二个最新出版物($latest_publications[1])。为什么会发生这种情况?
编辑:$stmt_autor = $db->conn->prepare($query_autor);
$stmt_autor->bind_param('i', $latest_publications[0]->getNumAuthor());
$stmt_autor->bind_result($nome_autor);
$stmt_autor->fetch();
$latest_publications[0]->setAuthor($nome_autor);
$stmt_autor = $db->conn->prepare($query_autor);
$stmt_autor->bind_param('i', $latest_publications[1]->getNumAuthor());
$stmt_autor->bind_result($nome_autor);
$stmt_autor->fetch();
$latest_publications[1]->setAuthor($nome_autor);
$stmt_autor = $db->conn->prepare($query_autor);
$stmt_autor->bind_param('i', $latest_publications[2]->getNumAuthor());
$stmt_autor->bind_result($nome_autor);
$stmt_autor->fetch();
$latest_publications[2]->setAuthor($nome_autor);
我建议你最好开始使用PHP的PDO
而不是mysql
扩展,它会为你节省很多麻烦,语法和错误处理更清晰:
的例子:
$DBH = new PDO('mysql:host=http://yourhost.com;dbname=yourdbname;charset=utf8', 'username', 'password');
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$STH = $DBH->prepare('SELECT * FROM `yourtable` WHERE `yourcolumn` = ?');
$STH->bindParam(1, $yourparam);
$STH->execute();
$STH->setFetchMode(PDO::FETCH_OBJ);
$mydata = array();
while ($var = $STH->fetch()) {
/* Retrieve the information */
$mydata[] = $var;
}
$DBH = null;
fetch()
方法将返回一个匿名对象(因为setFetchMode(PDO::FETCH_OBJ)
),并且它的字段名将被设置为您的列名。例句:
如果您的表有以下列:
id, name, date, address
您的对象将具有字段,以及查询的相应信息:
$obj->id
$obj->name
$obj->date
$obj->address
您可以在phpro.org的PDO教程页面上找到更多信息。
真正的OP在这里。我让我的朋友代我发了这个问题,因为我的学校IP屏蔽了我。所以很难及时得到适当的反馈。我终于解决了这个问题:
在展开之后,我提交了$stmt_autor->execute();
调用,在此之前我调用了 before $stmt_autor->bind_params();
因此出现了NULL/Empty作者名字段。循环中最后的、干净的工作代码片段:
$stmt_autor_med = $db->conn->prepare($query_autor);
if (!$stmt_autor_med) {
$code = $db->conn->errno;
$message = $db->conn->error;
printf("<p>SQL Error: %d %s</p>", $code, $message);
}
$stmt_autor_med->bind_param('i', $numeros_dos_autores[0]);
$stmt_autor_med->bind_result($nome_m);
if (!$stmt_autor_med->execute()) {
$code = $stmt_autor_med->errno;
$message = $stmt_autor_med->error;
printf("<p>Execution error: %d %s</p>", $code, $message);
} else {
$stmt_autor_med->fetch();
$ultimas_publicacoes_medicos[0]->autor = $nome_m;
}
$stmt_autor_med->free_result();
$stmt_autor_med->close();
变量名是葡萄牙语的,但您将获得基本信息。谢谢你的建议!