-4

I have the following two pieces of PHP code, both on the same page, but only the first one runs. What seems to be the problem?

The code:

div class="row" >
  <div class="col-md-12">

  enter code here
  <h1 id="username" style="color:white;text-align:center" >

    <?php
    if ($result->num_rows > 0){
  $row = $result->fetch_assoc();
   echo $row["username"];}
    ?>
    </h1>

</div>
</div>


<div class="row">
<div class="col-md-12">

    <h1 id="bash" style="color:blue;text-align:center">

    <?php
if ($result->num_rows > 0){
  $row = $result->fetch_assoc();
   echo $row["bash"];}

    ?>

    </h1>

</div>
</div>
Script47
  • 14,230
  • 4
  • 45
  • 66
  • try to `echo "test"` in the second php-part without the `if`, to check if you assumption is correct – webdeb Jul 11 '16 at 00:12
  • It is very likely this question will be closed as you have not provided enough information in regards to the issue. Is your error reporting enabled? If not why not? If yes are any errors showing? Could it be a query error? Have tried debugging or dumping your query data? Tell us what you are trying to do *vs* what is happening. – Script47 Jul 11 '16 at 00:12
  • 3
    Everytime you run `$result->fetch_assoc` you advance the cursor. `$row["bash"]` is the `bash` of row 2. Your question isn't very clear though, so that is just a guess.. – chris85 Jul 11 '16 at 00:14
  • 2
    You realize by running fetch_assoc() twice, you are attempting to get two different rows, right? – Devon Bessemer Jul 11 '16 at 00:14
  • @webdeb its works fine – MuhammadCST Jul 11 '16 at 00:46
  • @Script47 what i am trying to do is i have several column in the database and am trying to print the record inside each one with different style , Thats all – MuhammadCST Jul 11 '16 at 00:48
  • @Devon "Bash" and "username" is two different column in the database and am trying to print the record inside each one – MuhammadCST Jul 11 '16 at 00:50

1 Answers1

0

First of all, be sure to open and close your HTML tags properly. Then change your code to the following:

<?php
    $username = null;
    $bash = null;
    if ($result -> num_rows > 0) {
        $row = $result->fetch_assoc();
        $username = $row["username"];
        $bash = $row["bash"];
    }
?>


<div class = "row" >
    <div class = "col-md-12">
        enter code here
        <h1 id = "username" style = "color:white; text-align:center">
            <?= $username ?> <!-- OR --> <?php echo $username; ?>
        </h1>
    </div>
</div>


<div class = "row">
    <div class = "col-md-12">
        <h1 id = "bash" style = "color:blue; text-align:center">
            <?= $bash ?> <!-- OR --> <?php echo $bash; ?>
        </h1>
    </div>
</div>
  • There is absolutely no reason to repeat the same block of PHP code twice when you can execute it just once.
  • You can just initiate two variables outside the conditional statement and then change their values based on the result.
  • Then, you can just echo the variables in the h1 tags you want to respectively.

Side note: For <?= $variable ?> to work in versions < PHP 5.4, the short tags must be enabled in the php.ini file.

Angel Politis
  • 10,955
  • 14
  • 48
  • 66