gpt4 book ai didi

php - mysql_fetch_assoc 函数替换

转载 作者:行者123 更新时间:2023-11-29 08:24:39 26 4
gpt4 key购买 nike

在我的 php 代码中,我有 2 个循环,它们运行正常,除了 1 个 if 语句,我无法替换以下函数。

(1)虽然这段代码很好并且在第一个循环中运行

if(isset($_GET['Filter']) && !empty($_GET['Filter'])){
$query = "SELECT * FROM Apartment WHERE ";
foreach( $_GET['Filter'] as $filter ){
$query .= "$filter='y' AND ";
}
$query = substr($query, 0, -5);
}

while($data = mysql_fetch_array( $result )) {
//echo $data
}

(2)另一个给出了 sql 错误

if(isset($_GET['Filter']) && !empty($_GET['Filter'])){
$sql = mysql_query("SELECT * FROM Apartment WHERE" );
foreach( $_GET['Filter'] as $filter ){
$sql .=("$filter='y' ORDER BY RegularPrice ASC LIMIT 1 AND ");
}
$sql = substr($sql, 0, -5);
}

while($row = mysql_fetch_assoc($sql)) {
//echo $row
}

有人可以帮我找到我发布的第二个 if 语句中最终的语法错误吗?

最佳答案

对于第二个查询,您将部分 mysql_query() 分配给变量 $sql,然后将字符串连接到它。我认为这是一个错误,您只想将字符串 SELECT * FROM Apartment WHERE 分配给变量 $sql。

另外,请注意您要连接的字符串:

$sql = mysql_query("SELECT * FROM Apartment WHERE" );
foreach( $_GET['Filter'] as $filter ){
$sql .=("$filter='y' ORDER BY RegularPrice ASC LIMIT 1 AND ");
}

这将产生如下内容:

SELECT * FROM Apartment WHERE filter1='y' ORDER BY RegularPrice ASC LIMIT 1 AND filter2='y' ORDER BY RegularPrice ASC LIMIT 1 AND filter3='y' ORDER BY RegularPrice ASC LIMIT 1 AND

这是一个无效的 SQL 查询。

您想尝试什么:

$sql = "SELECT * FROM Apartment WHERE ";
foreach( $_GET['Filter'] as $filter ){
$sql .= "$filter='y' AND ";
}
$sql = substr($query, 0, -5); // Remove the last ' AND '
$sql .= " ORDER BY RegularPrice ASC LIMIT 1";

$result = mysql_query ($sql);

关于php - mysql_fetch_assoc 函数替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18290756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com