Barrie Reader
Barrie Reader

Reputation: 10713

Multiple MySQL/PHP Queries on one page, not playing fair!

I have the following MySQL Query:

<div class="box" id="settlement1">
        <?
            $query  = "SELECT name, pub, hospital, trade FROM settlements WHERE settlementID = 1";
            $result = mysql_query($query);

            while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $settlementName = $row['name'];
                $pubExists = $row['pub'];
                $hospitalExists = $row['hospital'];
                $tradeExists = $row['trade'];
            }
        ?>
        <div class="settlement-name"><? echo $settlementName; ?></div>
        <div class="settlement-contents">
            <?
                if ($hospitalExists == '1') { echo "<div class='hospital'>HOSPITAL</div>";  }
                if ($pubExists == '1') { echo "<div class='pub'>PUB</div>"; }
                if ($tradeExists == '1') { echo "<div class='trade'>TRADE</div>"; }
            ?>
        </div>
    </div>
    <div class="box" id="settlement2">
        <?
            $query  = "SELECT name, shop, hospital, trade FROM settlements WHERE settlementID = 2";
            $result = mysql_query($query);

            while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $settlementName = $row['name'];
                $pubExists = $row['pub'];
                $hospitalExists = $row['hospital'];
                $tradeExists = $row['trade'];
            }
        ?>
        <div class="settlement-name"><? echo $settlementName; ?></div>
        <div class="settlement-contents">
            <?
                if ($hospitalExists == '1') { echo "<div class='hospital'>HOSPITAL</div>";  }
                if ($pubExists == '1') { echo "<div class='pub'>PUB</div>"; }
                if ($tradeExists == '1') { echo "<div class='trade'>TRADE</div>"; }
            ?>
        </div>
    </div>

Now, the first query works fine, but the second returns the same results as the first.

ALL FIELDS ARE DIFFERENT IN QUERY 1 TO QUERY 2

^_^ <( HELP! )

Upvotes: 1

Views: 2191

Answers (2)

Tomasz Struczyński
Tomasz Struczyński

Reputation: 3303

I'm not sure if this is the cause but...

first SQL:

$query  = "SELECT name, pub, hospital, trade FROM settlements WHERE settlementID = 1";

Second:

$query  = "SELECT name, shop, hospital, trade FROM settlements WHERE settlementID = 2";

in first one there's pub, in second one - shop. But you don't use shop value in PHP in second case, only pub (which is not in this SQL fields.

You may have SQL error here (no field in database) or PHP error (checking non-existent field in result array).

Upvotes: 1

Thariama
Thariama

Reputation: 50832

Could it be that you reuse the variable $query or $result further down leading to output the result of the first $query or to execute the same query again?

Upvotes: 0

Related Questions