Php Mysql没有返回任何行
我的问题是,我没有从mysql获得任何产品。如果我回显sql查询,我会返回3行,这很好,因为我有3个产品属于ID-12类别 如果回显$category_id,我得到:12 我的代码怎么了?我没有犯任何错误 谢谢大家!Php Mysql没有返回任何行,php,mysql,Php,Mysql,我的问题是,我没有从mysql获得任何产品。如果我回显sql查询,我会返回3行,这很好,因为我有3个产品属于ID-12类别 如果回显$category_id,我得到:12 我的代码怎么了?我没有犯任何错误 谢谢大家! <?php function render_category_page($category_id) { $id = (int)$this->$category_id; $connect_to_mysql = my_conn(); if($con
<?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\u product\u data()
需要两个参数
获取产品数据()
返回一些数据
您的代码中有两个问题:
get_product_数据(“从产品中选择*,其中category_id=”“$id.””,$connect_to_mysql,$limit=0)代码>
查询执行的结果不会保存在任何变量中,这使它变得无用
if(get_product_data())
,此处不传递任何参数,因此您的代码进入else
代码的主体
这也是你需要做的
- 将查询结果保存在变量中,然后使用该变量检查
if
条件。大概是这样的:
$result 1=get_product_data(“从产品中选择*,其中category_id=”“$id.””,$connect_to_mysql,$limit=0);
如果($result1){
...
- 或者,您可以将这两条语句组合在一行中,如下所示:
if(获取产品数据(“从类别id所在的产品中选择*)=
“$id.”,$connect_to_mysql,$limit=0)){…
希望这有助于您理解您的错误。get_product_data是如何定义的?这是一个函数,但有时是值?您在这里使用了很多函数,如果没有看到这些函数,我们就不可能说出它在哪里失败。您可能在这里变得复杂。黄瓜,或者什么意思?您正在铸造$category_id
到int