我有此代码,它工作正常,但我想将具有代码的内容更改为另一种结构 [英] I have got this code it is working fine but I want to change the having code into another structure

查看:68
本文介绍了我有此代码,它工作正常,但我想将具有代码的内容更改为另一种结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前可以正常使用的代码:

This is the code I have at the moment which works fine:

  <?php
    $sql = "SELECT * FROM te_events order by eventTitle ASC ";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()) 
    {
        $venueID = $row['venueID'];
        $catID = $row['catID'];

        $sql2 = "SELECT * FROM te_venue where venueID='$venueID'";
        $result2 = $conn->query($sql2);

        while($row2 = $result2->fetch_assoc()) 
        {
            $venueName = $row2['venueName'];
        }

        $sql3 = "SELECT * FROM te_category where catID='$catID'";
        $result3 = $conn->query($sql3);

        while($row3 = $result3->fetch_assoc()) 
        {
            $catName = $row3['catDesc'];
        }

?>

但是我想将其更改为这种格式.我只能做不到这一点,直到得到错误为止.

But I want to Change it into this format. I could do only till this bit couldn't go further than this I get errors.

<?php
    $sql ="SELECT eventTitle, eventID, venueID, catID, eventStartDate, eventEndDate, eventPrice FROM te_events ORDER BY eventTitle ASC";
    $queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    while ($row = mysqli_fetch_array($queryresult)) {

        $venueID = $row['venueID'];
        $catID = $row['catID'];
        $venueName = $row['venueName'];
        $catName = $row['catDesc'];

?>

那我该怎么办?
如何连接两个表?

How can I do that then?
how can I join two tables?

推荐答案

您应该能够 join 另外2个表以获取所需的列.

You should be able to join the additional 2 tables to get the columns you need.

SELECT e.eventTitle, e.eventID, e.venueID, e.catID, e.eventStartDate, e.eventEndDate, e.eventPrice, v.venueName, c.catDesc
 FROM te_events as e
join te_venue as v
on e.venueID = v.venueID
join te_category as c
on c.catID = e.catID
ORDER BY eventTitle ASC

您还应该避免将数据直接放入查询中.如果需要这样做,请使用参数化查询.这就是SQL注入(或第二级)发生的方式.

You also should avoid putting data directly into a query. If you need to do that use parameterized queries. This is how SQL injections (or second level) occur.

这篇关于我有此代码,它工作正常,但我想将具有代码的内容更改为另一种结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆