内部连接数据库
Connect database internally
我在数据库中有 three tables。
1-引号
2种
3 位作者
在引号 table 中,我分配了 type_id,它是类型 table 的主键,还有 author_id,它是作者 table 的主键。
现在,当我想以这种格式一次回显一个引号时。
"When you get to my age life seems little more than one long march to
and from the lavatory."
By: A. C. Benson Category: age
那么如何连接这个数据库来获得这种类型的输出呢?
我试过了,但它只输出报价。
$sql = "SELECT * FROM quotes LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo " \" " . $row["quote"]. "\"<br><br>";
}
您应该使用 JOIN 子句将引文的输出与类型和作者联系起来。
例如,如果每个引用都有一个字段 type_id 和 author_id 到 link 到其他表。
SELECT * FROM quotes JOIN types ON quotes.type_id = types.id JOIN authors ON quotes.author_id = author.id
在 PHP 中可能如下所示:
$sql = "SELECT * FROM quotes JOIN types ON quotes.type_id = types.id JOIN authors ON quotes.author_id = authors.id";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo " \" " . $row["quote"]. "\"<br>".$row["author"]." ".$row["type"] ."<br>";
}
当您的表格看起来像这样时:
quotes:
id, quote, author_id, type_id
types:
id, type
authors:
id, author
我在数据库中有 three tables。
1-引号
2种
3 位作者
在引号 table 中,我分配了 type_id,它是类型 table 的主键,还有 author_id,它是作者 table 的主键。
现在,当我想以这种格式一次回显一个引号时。
"When you get to my age life seems little more than one long march to
and from the lavatory."
By: A. C. Benson Category: age
那么如何连接这个数据库来获得这种类型的输出呢?
我试过了,但它只输出报价。
$sql = "SELECT * FROM quotes LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo " \" " . $row["quote"]. "\"<br><br>";
}
您应该使用 JOIN 子句将引文的输出与类型和作者联系起来。
例如,如果每个引用都有一个字段 type_id 和 author_id 到 link 到其他表。
SELECT * FROM quotes JOIN types ON quotes.type_id = types.id JOIN authors ON quotes.author_id = author.id
在 PHP 中可能如下所示:
$sql = "SELECT * FROM quotes JOIN types ON quotes.type_id = types.id JOIN authors ON quotes.author_id = authors.id";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo " \" " . $row["quote"]. "\"<br>".$row["author"]." ".$row["type"] ."<br>";
}
当您的表格看起来像这样时:
quotes:
id, quote, author_id, type_id
types:
id, type
authors:
id, author