dongzhao4036 2015-05-08 16:06
浏览 39
已采纳

从数据库的两个表中检索数据并使用php显示它[关闭]

I have created a database named (insert) having two tables table1 and table 2.

table1 have 3 columns user, id, roll.

table2 have 2 columns id, menu.

I have inserted values in two tables. I want to retrieve all values from two tables and show them in another php file, but my query is not working. It's showing error. What should I do? here is my code

<?php

include 'insertjoincon.php';
$sql = "SELECT table1.user, id FROM table1, table2 WHERE    table1.id=table2.id";

echo "<table border=1>
<tr><th>Name</th><th>ID</th></tr>
";

while ($line = mysql_fetch_array($sql)) {
    $name = $line["user"];
    $id = $line["id"];
    echo "<tr>";
    echo "<td>" . $name . "</td>";
    echo "<td>" . $id . "</td>";
    echo "</tr>";

    echo "</table>";
}

$conn->close();
?>
  • 写回答

3条回答 默认 最新

  • douchui4459 2015-05-08 16:45
    关注

    (Edit: The first few sentences are incorrect. In my first glance I saw that you specified a table for one column but missed that you didn't specify a table for id. Because they share the same value, pick one table and specify it. You should always specify table with either explicit or implicit JOINS. Still, first thing's first).

    I'm guessing your query actually works if you test it. There is nothing inherently wrong with your query (although I always recommend using explicit JOINS).

    There is a good reason why the example code you're providing would not work...

    You're missing $result = mysqli_query($sql):

    Yok can then use mysqli_fetch_array($result).

    mysqli_fetch_array() expects a result, not a SQL string.

    Please note that you should use mysqli not mysql.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系