Rickshaw
Rickshaw

Reputation: 107

Calling two querys in mysql_fetch_array?

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. :\

Upvotes: 1

Views: 253

Answers (2)

KarelG
KarelG

Reputation: 5244

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.

Upvotes: 2

Alesfatalis
Alesfatalis

Reputation: 777

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.

Upvotes: 1

Related Questions