1

Afternoon everyone, quick question on this fetch query.

<?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>

Anyone see anything wrong ere? The query fetches fine but, not properly, it will fetch parts of the other query even when the forms not even submitted. :\

5
  • 1
    you are over-writing $row with 2 fetch in the same condition !! Commented Mar 6, 2014 at 14:30
  • Hmm, I see. Is there no way of writing while loop ere without changing $row? Or am I heading in the wrong direction? Commented Mar 6, 2014 at 14:34
  • well the while loop with 2 fetch does not make sense.. what if first query returns 10 row and second 5 so as per the condition the loop will break at 5 so if you want to display 10 data from first query you can not with the above condition. Commented Mar 6, 2014 at 14:37
  • just out of curiosity, can you not execute your two queries in one ? Then you have one fetch to handle. It is also faster and you would avoid data loss ( like if $query has 5 and $query1 has 10, then 5 products will be displayed, ignoring other 5 from $query1. Commented Mar 6, 2014 at 14:45
  • I think it is possible, but clearly it has been proven difficult for me :\ Commented Mar 6, 2014 at 14:48

2 Answers 2

2

You can run the query one time. Just combine the GET and POST when creating a query.

$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)) { ?>

Although i have to add a remark. You are using mysql() functions, which is officially deprecated. I should check PDO for using db from PHP.

Sign up to request clarification or add additional context in comments.

Comments

1

Try using to different fetch variables:

 <?php while ($row = mysql_fetch_assoc($query) and $row2 = mysql_fetch_assoc($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>

Use for example $row for the first and $row2 for the second query.

If you want only $row in my opinion than you have to build a query which outputs the result of query1 and query2

First run your query in your if statements and the output too. Try this code:

<?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());
            while ($row = mysql_fetch_assoc($query)) { ?>
        <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>
<?php
        } 
    }

        $sql1 = "SELECT * FROM products ";
        if(isset($_GET['q'])){
            $categories = mysql_real_escape_string($_GET['q']);
            $sql .= "WHERE type LIKE '%$categories%'";
            $query = mysql_query($sql1) or die(mysql_error());
            while ($row = mysql_fetch_assoc($query)) { ?>
                <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>
<?php           
        }   

     ?>

I am not sure if this works like you expected but give it a try.

6 Comments

build a query which outputs the result of query1 and query2 This!
Ah, was really aiming to avoid that :(
@Rickshaw i don't know your querys but those are the two options you have ;)
Here is the query, but it's pretty difficult putting those querys together since one is based $_POST while the other one is $_GET :(
@Rickshaw edited my answer. Hope this is the solution :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.