Php 内部连接数据库
我有一个数据库 单引号 2型 三作者 在quotes表中,我分配了type_id,它是types表中的主键,也分配了AUTHER_id,它是AUTHERS表的主键 现在,当我想以这种格式一次性回显一个报价时Php 内部连接数据库,php,mysql,database,jointable,Php,Mysql,Database,Jointable,我有一个数据库 单引号 2型 三作者 在quotes表中,我分配了type_id,它是types表中的主键,也分配了AUTHER_id,它是AUTHERS表的主键 现在,当我想以这种格式一次性回显一个报价时 "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 那么,如何连接这个数据库以获得
"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>";
}
$sql=“选择*来自报价限制1”;
$result=$con->query($sql);
如果($result->num_rows>0){
而($row=$result->fetch_assoc()){
回显“\”.$row[“quote”]。“\”
”;
}
您应该使用JOIN子句将引号中的输出与类型和作者连接起来
例如,如果每个引号都有一个字段类型id和作者id来链接到其他表,则可能是这样
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表的结构?是的类别表示类型,我有链接图像,然后您可以使用
$row[“column\u name”]
中每个连接表的所有行名称。如果它们具有相同的名称,例如,作者有一个“name”列,quote有一个“name”列您可以使用别名。您还可以尝试使用别名选择quotes.quote、authors.author作为作者名、types.type作为类型名,从quotes上的quotes连接类型。type\u id=types.id在quotes上连接作者。author\u id=authors.id,然后尝试$row[“author\u name”]
当我在phpmy admin中运行查询时,SQL中的一切都很好,这是完美的。但在我的php文件中,它给出了错误:(嗯,您可以尝试使用fetch\u array
而不是fetch\u assoc
,但这不会有什么区别。您可以使用fetch字段列出列名,看看这可能会显示列是否到达php端
quotes:
id, quote, author_id, type_id
types:
id, type
authors:
id, author