-2

我的问题是,我没有从 mysql 获得任何产品。如果我回显 sql 查询,我会返回 3 行,这很好,因为我在 ID-12 类别中有 3 个产品。

如果回显 $category_id,我得到:12

我的代码有什么问题?我没有得到任何错误。

谢谢!

<?php
function render_category_page($category_id)
{
    $id = (int)$this->$category_id;
    $connect_to_mysql = my_conn();

    if($connect_to_mysql == true )
    {
        get_product_data("SELECT * FROM products WHERE category_id = '".$id."'", $connect_to_mysql, $limit = 0);
        if(get_product_data())
        {
            if(count_rows_of_sql(get_product_data) == 0 )
            {
                show_message("There are no rows in the table.");
            }
            else
            {
                show_message("There are '".count_rows_of_sql(get_product_data)."' rows in the table.");

                $product = fetch_data_from_sql(get_product_data());

                $category_id            = (int)$product['category_id'];
                $product_name           = "<h1 class='product_name'>".cut_html_chars($product['product_name'])."</h1>";
                $product_desricption    = "<p class='product_desricption'>".cut_html_chars($product['product_desricption'])."</p>";
                $product_full_content   = "<div class='product_full_content'>".cut_html_chars($product['product_full_content'])."</div>";
                $product_to_link        = "<a href='".$this->$product_to_link."' class='product_to_link' title='".$this->$product_name."' target='_self'>".$this->$product['product_seo_url']."</a>";

                for($i = 1; $i <= count_rows_of_sql(get_product_data); $i++)
                {
                    $i++;
                }

                $products_in_category = (int)products_in_category($i);
            }
        }
        else
        {
            log_message("The sql command didnt run.", $type = "sql", $write_to_file = 1);
            write_error_message_to_file("./logs/error_logs/", $filename = $type . "-error" . date("Y-m-d-H:i:s") . generate_random_number($min = 1, $max = 9999), $fileType = "txt");
        }
    }
    else
    {
        log_message("Cant connect to database.", $type = "sql", $write_to_file = 1);
        write_error_message_to_file("./logs/error_logs/", $filename = $type . "-error" . date("Y-m-d-H:i:s") . generate_random_number($min = 1, $max = 9999), $fileType = "txt");
    }
}
render_page($page_type = "category", $html = 1, $template = "bootsrap-4", $pagination = $products_in_category, $product_container_div  = "product-container", render_category_page($category_id));
?>
4

1 回答 1

0

首先,你做了太多的功能。并在其中犯了几个错误。看了你的代码,我明白了:

  • get_product_data()需要几个论点
  • get_product_data()返回一些数据

您的代码中有两个问题:

  • get_product_data("SELECT * FROM products WHERE category_id = '".$id."'", $connect_to_mysql, $limit = 0); 查询执行的结果没有保存在任何变量中,使其无用。

  • if(get_product_data()),这里没有传递任何参数,因此您的代码进入了else代码主体

这是你需要做的,要么

  • 将查询结果保存在变量中,然后使用该变量检查if条件。像这样的东西:

    $result 1 = get_product_data("SELECT * FROM products WHERE category_id = '".$id."'", $connect_to_mysql, $limit = 0); if($result1) { ...

  • 或者,您可以将这两个语句组合在一行中,如下所示: if(get_product_data("SELECT * FROM products WHERE category_id = '".$id."'", $connect_to_mysql, $limit = 0)) { ...

希望这可以帮助你理解你的错误。

于 2020-06-16T09:22:42.170 回答