【问题标题】:Accessing second table within a while loop在while循环中访问第二个表
【发布时间】:2012-07-20 21:02:57
【问题描述】:

请原谅我这周刚学了php,所以我不确定我是否做得很好。

它开始访问数据库和标题的类别表;然后它会获取该信息并创建标题、定价和目录链接。

然后在该 while 循环中,在它完成第一部分后,它应该运行第二个 while 循环来访问 products 表,以列出 category_id 与类别表中的 cat_id 匹配的所有产品。

当它打印出来时应该是

标题
定价 PDF
商品尺寸 图片 图片
商品尺寸 图片 图片
商品尺寸 图片 图片
等等

标题
定价 PDF
商品尺寸 图片 图片
等等……

到目前为止,第一个 while 循环有效,但第二个无效。是否有正确的方法来传递变量?我可以在第一个表的 while 循环中不访问第二个表吗?我不知道...我已经尝试了一些东西,但没有一个效果很好

<?php
//connect to server
$con = mysql_connect('localhost','username','password');
//test connection
if (!$con)
{
    die ('Could not connect: ' . mysql_error());
}

//access primary DB 
mysql_select_db("main_db", $con);

//place table into variable
$categories = mysql_query("SELECT * FROM categories");


//begin table build
while($row = mysql_fetch_array($categories))
{
    //set shading variable
    $table_row = 0;

    //set current set   
    $cur_set = $row['cat_id'];
    //create document link and header
    echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
    //create table and table formatting cell
    echo "<table id='productTable'><tr id='tableHead'>";
    //table width formattting here
    echo "<td style='width:165px;'></td>";
    echo "<td style='width:235px;'></td>";
    echo "<td style='width:155px;'>";
    //link and icons to category catalog
    echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
    //link and icons to category pricing sheet
    echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
    //finish formatting
    echo "</td></tr>";

    //place table into variable
    $products = mysql_query("SELECT * FROM products WHERE category_id='" . $row['cat_id'] . "'");

    //begin table build
    while($table = mysql_fetch_array($products));
    {
        //create up row
        echo "<tr id='tr" . $table_row . "'>";
        //create first cell
        echo "<td>" . $table['prod_name'] . "</td>";
        //create second cell
        echo "<td>" . $table['prod_dim'] . "</td>";
        //create third cell
        echo "<td>";
        //create third cell, first image
        echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
        //create third cell, second image
        echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
        //finish formatting
        echo "</td></tr>";
        //cycle row
        if ($table_row == 0)
            {
                    $table_row = 1;
            }
        else
            {
                $table_row = 0;
            }

    //end table
    echo "</table>";
    }
}

//close connection
mysql_close($con);
?>

提前致谢

【问题讨论】:

  • 请不要将mysql_* 函数用于新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,here is good PDO tutorial.
  • 看看 SQL 中的 JOIN 查询。你可以在一个循环中达到同样的效果。
  • 他可以做到@Romain 和 Rolando 所说的那样,但首先因为他是 php 新手,所以他应该逐步学习,对吗?
  • php 和 mysql 新手;一直在 w3schools 上学习。在我来到这里并开始寻找其他有类似问题的人之前,我什至没有听说过这个 PDO/MySQLi 的东西。

标签: php mysql database


【解决方案1】:

在两个表上执行 INNER JOIN 会更精简

SELECT
    A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,
    B.prod_name,B.prod_img1,B.prod_img2
FROM categories A INNER JOIN products B ON A.cat_id = B.category_id;

您可以在 A.cat_id 上进行迭代

这是我提出的建议(大括号可能已关闭,但 cat_id 上的迭代应该是这样的)。请更改开始和停止标签的样式。

<?php
//connect to server
$con = mysql_connect('localhost','username','password');
//test connection
if (!$con)
{
    die ('Could not connect: ' . mysql_error());
}

//access primary DB 
mysql_select_db("main_db", $con);

//place table into variable
$categories = mysql_query("SELECT A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,B.prod_name,B.prod_img1,B.prod_img2 FROM categories A INNER JOIN products B ON A.cat_id = B.category_id");

$current_catid = -1;

//begin table build
while($row = mysql_fetch_array($categories))
{
        if ( $current_catid != $row['cat_id'] )
        {
            if ( $current_catid > -1 ) { echo "</table>"; }
            $current_catid != $row['cat_id']

    //set shading variable
    $table_row = 0;

    //set current set   
    $cur_set = $row['cat_id'];
    //create document link and header
    echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
    //create table and table formatting cell
    echo "<table id='productTable'><tr id='tableHead'>";
    //table width formattting here
    echo "<td style='width:165px;'></td>";
    echo "<td style='width:235px;'></td>";
    echo "<td style='width:155px;'>";
    //link and icons to category catalog
    echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
    //link and icons to category pricing sheet
    echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
    //finish formatting
    echo "</td></tr>";
        }

//create up row
    echo "<tr id='tr" . $table_row . "'>";
    //create first cell
    echo "<td>" . $table['prod_name'] . "</td>";
    //create second cell
    echo "<td>" . $table['prod_dim'] . "</td>";
    //create third cell
    echo "<td>";
    //create third cell, first image
    echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
    //create third cell, second image
    echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";

    //finish formatting
    echo "</td></tr>";
        //cycle row
    if ($table_row == 0)
    {
        $table_row = 1;
    }
    else
    {
        $table_row = 0;
    }

    //end table (Fix this, might produce extra table tag)
    echo "</table>";
}

//close connection
mysql_close($con);
?>

【讨论】:

  • 请原谅我,我是一个非常有眼光的人,我只是不知道这个内部连接是如何工作的。它基本上就像在数组内部创建一个数组吗?另外,就你重组它的方式而言,你让它不断运行 while 循环,并且 if 语句在第一次传递时创建类别标题是否正确?
  • 将类别和产品放在一起是 1 个查询。以最坏的情况,12个类别和20个产品。该查询将生成 240 行。你只需要连接一次mysql。使用您的原始代码,您将连接 13 次(一次连接到类别,12 次连接到产品)。更有效的检索。手动运行查询并查看输出。
  • JOIN 就像数组中数组的二维表示。示例:Table1 有 (a,b,c),Table2 有 (1,2,3,4)。 INNER JOIN 产生 (a,1),(a,2),(a,3),(a,4),(b,1),(b,2),(b,3),(b,4 ),(c,1),(c,2),(c,3),(c,4)。
  • 这让我很头疼,但我会调查一下;感谢您的宝贵时间。
【解决方案2】:

您正在加载相关数据,因此简短的回答是否定的,因为如果您使用连接选择它们可能会产生开销。

此外,请尝试通过仅选择您想要的而不是所有内容来限制由 * 产生的开销。

对于新的 mysql(i) 调用,也要遵循 Truth 的建议。一旦你掌握了 sql 和 php,你可以转移到其他类型的数据库调用(使用面向对象的代码 - 或 activerecord),但请先尝试了解 mysql 调用是如何完成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2012-08-11
    • 1970-01-01
    • 2012-04-01
    • 2019-10-13
    相关资源
    最近更新 更多