PHP中的多个MySQL查询无法正常工作

时间:2019-06-05 13:36:39

标签: php mysql

对于用于从MySQL表中请求数据的搜索算法,我具有以下代码。

function checkValue($value){
    if($value == 'undefined'){
        $value = '%%';
        return $value;
    }else{
        $value = '%'. $value .'%';
        return $value;
    }
}

$source = $_GET['origin'];
$data = array();


if($source == 'search'){

    $result = mysqli_query($con, "select * from products where gender = '". $_GET['gender'] ."' and product_name like '". checkValue($_GET['name']) ."' and product_brand like '". checkValue($_GET['brand']) ."' and price > ". $_GET['minPrice'] ." and price < ". $_GET['maxPrice'] );

    $sel = array();
    while ($row = mysqli_fetch_array($result)){
        $sel[] = mysqli_query($con, "select * from products where product_no = '". $row['product_no'] ."' and category like '". checkValue($_GET['cat']) ."' or category like '". checkValue($_GET['cat1']) ."' or category like '". checkValue($_GET['cat2']) ."' ");
    }

    while ($row = mysqli_fetch_array($sel)){
        if($row['sale'] == 'true'){
            $data[] = array("name"=>$row['product_name'],"brand"=>$row['product_brand'],"price"=>$row['price'], "img_url"=>$row['img'], "href"=>$row['href'], "sale"=>$row['sale'], "pre_price"=>$row['pre_price'], "post_price"=>$row['post_price'],  "percentage_discount"=>$row['percentage_discount'] );
        }else{
            $data[] = array("name"=>$row['product_name'],"brand"=>$row['product_brand'],"price"=>$row['price'], "img_url"=>$row['img'], "href"=>$row['href'], "sale"=>$row['sale'], "pre_price"=>$row['price'], "post_price"=>$row['price'],  "percentage_discount"=>0);
        }
    }

}

此代码以前运行正常,没有第二个命令。我试过在同一个查询中应用这两个查询,尽管这会使我的结果无效。数据库已配置,连接正常。 我在控制台中收到一个内部服务器错误,尽管这不是语法错误,因为文件中的所有其他查询都可以正常工作。

我是php的新手,但是当我在使用mysql-connector的python 3中遇到类似的问题时,我已经使用了缓冲的游标,在php中是否有等效功能?

2 个答案:

答案 0 :(得分:0)

我建议在此处使用“自我连接”以获取您的数据以供参考,您可以与此link一起使用。

示例:

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

答案 1 :(得分:-1)

函数mysqli_fetch_array()期望从数据库查询中获得Resource,但是您传递了array的资源。

要保留代码,只需将查询分配到变量$result2中,该变量将保留指向查询结果的指针,然后可以使用mysqli_fetch_array()进行遍历。

    $result = mysqli_query($con, "select * from products where gender = '". $_GET['gender'] ."' and product_name like '". checkValue($_GET['name']) ."' and product_brand like '". checkValue($_GET['brand']) ."' and price > ". $_GET['minPrice'] ." and price < ". $_GET['maxPrice'] );

    $sel = array();

    while ($row = mysqli_fetch_array($result)){

        $result2 =  mysqli_query($con, "select * from products where product_no = '". $row['product_no'] ."' and category like '". checkValue($_GET['cat']) ."' or category like '". checkValue($_GET['cat1']) ."' or category like '". checkValue($_GET['cat2']) ."' ");

        while ($row2 = mysqli_fetch_array($result2)){
        if($row2['sale'] == 'true'){
            $data[] = array("name"=>$row2['product_name'],"brand"=>$row2['product_brand'],"price"=>$row2['price'], "img_url"=>$row2['img'], "href"=>$row2['href'], "sale"=>$row2['sale'], "pre_price"=>$row2['pre_price'], "post_price"=>$row2['post_price'],  "percentage_discount"=>$row2['percentage_discount'] );
        }else{
            $data[] = array("name"=>$row2['product_name'],"brand"=>$row2['product_brand'],"price"=>$row2['price'], "img_url"=>$row2['img'], "href"=>$row2['href'], "sale"=>$row2['sale'], "pre_price"=>$row2['price'], "post_price"=>$row2['price'],  "percentage_discount"=>0);
        }
        }
    }