Caro_deb
Caro_deb

Reputation: 279

PHP mysql: 2 similar queries echoes results of 2nd query only

The result of the 2nd query overwrites the result of the 1st query. I've never run into a similar issue before.

mysql table:

updateid    picture  movie  
  14          1       1  
  22          0       1  
  33          1       0

php file:

<div id="imagecheck">
<?php 
$image_check= query("SELECT picture FROM list WHERE updateid = '$updateid' ");
foreach ($row as $image_check);

if (!($image_check))
{
?>

    <div class="result" id="camera<?php echo $updateid ?>">FOO</div>

<?php
}
else 
{
    ?>
    <div class="result" id="camera<?php echo $updateid ?>">BAR</div>

    <?php
}

?>
</div>
<div id="moviecheck">

  <?php 
  $movie_check = query("SELECT movie FROM list WHERE updateid = '$updateid' "); 
  foreach ($row as $movie_check);
    if  (!($movie_check))
    { 
    ?>
 <div class="result1" id="movie<?php echo $updateid ?>">FOO</div>

<?php
}
else 
{
    ?>
    <div class="result1" id="movie<?php echo $updateid ?>">BAR</div>

    <?php
}

?>

when i run those 2 queries, the result for

14 echoes BAR BAR

22 echoes FOO FOO

33 echoes FOO FOO

Any ideas what might go wrong here ?

Upvotes: 0

Views: 72

Answers (1)

Marc B
Marc B

Reputation: 360572

Probably typos. you're using variable variables:

  foreach ($row as $$movie_check);
                   ^^--- note the doubled $

e.g.

$x = 'foo';
$foo = 'bar';
echo $$x; // outputs 'bar'
echo $x; // outputs 'foo'

As a general rule, never EVER use variable variables. They make for utterly unmaintainable and nearly impossible-to-debug code.

Upvotes: 4

Related Questions