duanlao1552 2017-05-28 19:02
浏览 47
已采纳

使用PHP从Relational数据库获取数据时出错

I have designed a simple relational database. When I am trying to get the data from the server it is throwing an error: (I have skipped some code to make it simple)

This is the SQL syntax I am using:

$sql = "SELECT lead.id, lead.name, lead.phone, lead.email, treatment.name, source.name, status.name FROM lead join treatment join source join status on treatment.id = lead.treatment_id and source.id = lead.source_id and status.id = lead.status_id";

This is used inside HTML:

echo "
<tr>
<td>".$row["lead.id"]."</td>
<td>".$row["lead.name"]."</td>
<td>".$row["lead.email"]."</td>
<td>".$row["treatment.name"]."</td>
<td>".$row["source.name"]."</td>
<td>".$row["status.name"]."</td>
</tr>";

This code is giving an error, when I change $row["lead.id"] to $row["id"] it works but I need to mention the table name as I have same column names in almost all the tables.

is there any way to do it using the table name?

</div>
  • 写回答

1条回答 默认 最新

  • doz97171 2017-05-28 19:06
    关注

    You have the on condition in wrong place and with incorrect and condition you should use on condition for each table

    $sql = "SELECT 
              lead.id
            , lead.name
            , lead.phone
            , lead.email
            , treatment.name
            , source.name
            , status.name 
          FROM lead 
          join treatment on treatment.id = lead.treatment_id
          join source on source.id = lead.source_id
          join status on status.id = lead.status_id";
    

    and for index try using alias avoiding table name and dot notation

      $sql = "SELECT 
                lead.id as lead_id
              , lead.name as lead_name
              , lead.phone as lead_phone
              , lead.email as lead_email 
              , treatment.name as treatment_name_
              , source.name  as  source_name
              , status.name  as status_name
            FROM lead 
            join treatment on treatment.id = lead.treatment_id
            join source on source.id = lead.source_id
            join status on status.id = lead.status_id";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧