php - Mysql 没有返回任何行
问题描述
我的问题是,我没有从 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));
?>
解决方案
首先,你做了太多的功能。并在其中犯了几个错误。看了你的代码,我明白了:
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)) { ...
希望这可以帮助你理解你的错误。
推荐阅读
- wordpress - 在 Wordpress 的页面上创建有效的 RSS 提要
- d3.js - d3.js - 在强制导向图中 alpha < 0.5 后停止内部刻度
- python - Pandas 快速迭代行的最佳方法
- redirect - 网址将第一个路径段重写为子域
- bash - 使用 run-this-one 运行的 Linux 脚本不适用于 docker
- django - Django Redirect 返回 200,但页面不重定向
- c++ - 找不到 CMAKE_Fortran_COMPILER
- ssl - 强制浏览器检索新 SSL 证书的最佳方法是什么?
- laravel - Laravel Nova - 如何修改度量值和分区以查询镜头数据视图
- javascript -