">" height="-6ren">
gpt4 book ai didi

php - 在 mysql_fetch_array 中调用两个查询?

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

大家下午好,关于这个获取查询的快速问题。

<?php
$sql = "SELECT * FROM products ";
if(isset($_POST['Submit'])){
if(empty($_POST['Search'])){
$error = true;
}else{
$searchq = mysql_real_escape_string($_POST['Search']);
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";
}
} $query = mysql_query($sql) or die(mysql_error());

$sql1 = "SELECT * FROM products ";
if(isset($_GET['q'])){
$categories = mysql_real_escape_string($_GET['q']);
$sql1 .= "WHERE type LIKE '%$categories%'";
} $query1 = mysql_query($sql1) or die(mysql_error());

?>





<?php while ($row = mysql_fetch_array($query) and $row = mysql_fetch_array($query1)) { ?>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><b><?php echo $row['name']?></a></div>
<div class="product_img"><a href="productsview.php<?php echo '?id='.$row['serial']; ?>"><img src="<?php echo $row['picture']?>" height="100" width="120" /></a></div>
<div class="prod_price"><span class="reduce"><?php if($row['rprice'] > 0) {echo "£"; echo $row['rprice'];}?></span> <span class="price"><big style="color:green">£<?php echo $row['price']?></big></span></div>
</div>
<div class="bottom_prod_box"></div>
<div align="center" class="prod_details_tab"> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </div>
</div>
<?php } ?></p>

有人看到这里有什么不对吗?查询获取很好,但不正确,即使表单甚至没有提交,它也会获取其他查询的部分内容。 :\

最佳答案

您可以运行查询一次。只需在创建查询时组合 GETPOST

$noWhere = true;
$sql = "SELECT * FROM products ";

if(isset($_POST['Submit'])){
if(empty($_POST['Search'])){
$error = true;
}else{
$searchq = mysql_real_escape_string($_POST['Search']);
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";
$noWhere = false;
}
}
// check if categories got sent
if(isset($_GET['q'])){
$categories = mysql_real_escape_string($_GET['q']);
if($noWhere) $sql .= "WHERE type LIKE '%$categories%'";
else $sql .= " OR type LIKE '%$categories%'";
}
$query = mysql_query($sql) or die(mysql_error());

?>

<?php while ($row = mysql_fetch_array($query)) { ?>

虽然我必须添加一个评论。您正在使用正式弃用的 mysql() 函数。我应该检查 PDO从 PHP 使用数据库。

关于php - 在 mysql_fetch_array 中调用两个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227404/

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