0

code:

<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
    <div class="box2">

        <?php 
            //session_start();  
            $sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
            include 'php/dbconnection.php';
            $conn = OpenCon();  //$conn=$_SESSION['conn'];
            $result = mysqli_query($conn,$sql);

            if($result){
                $rows = mysqli_fetch_array($result);

                echo "<div class='wrapper tab-content'>";
                echo "<section class='col1'>";

                echo "<h4><span>" . $rows['date']. "</span> </h4>";
                echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
                echo "<p class='pad_bot1'>".$rows['des'] ."</p>";

                echo "</section>";
                echo "</div>";
            }
        ?>
        </div>

     </div>

</article>
<!-- /content-->

Here I'm trying to, print 'news' table from database, but I'm unable to print anything on webpage. can anyone point out, what is wrong in this ? Thank You in advance for any help!

2
  • make the connection to db, before the sql select. Commented Aug 8, 2017 at 7:52
  • yes did, yet no output Commented Aug 8, 2017 at 8:02

3 Answers 3

1

You have to put mysqli_fetch_row($result) inside a loop.

Example with your case:

if($result){
    while ($rows=mysqli_fetch_row($result)){

        echo "<div class='wrapper tab-content'>";
        echo "<section class='col1'>";

        echo "<h4><span>" . $rows['date']. "</span> </h4>";
        echo "<p class='pad_bot2'><strong>". $rows['title']. "
        </strong></p>";
        echo "<p class='pad_bot1'>".$rows['des'] ."</p>";

        echo "</section>";
        echo "</div>";
    }

}
// Free result set
mysqli_free_result($result);

Hope this helps!

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

Comments

0

For commented $conn=$_SESSION['conn'];

You cannot store resource handles within a $_SESSION. See PHP.net:

Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object).

As your object contains the connection link it can't be serialized, Just reconnect with every request.

And you can make clean template like below

<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
    <div class="box2">
        <?php 
            //session_start();  
            $sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
            include 'php/dbconnection.php';
            $conn = OpenCon(); 
            $result = mysqli_query($conn,$sql);
            if($result):
            while ($row=mysqli_fetch_row($result)):
         ?>
           <div class='wrapper tab-content'>
             <section class='col1'>
                <h4><span><?php echo $row['date'];?></span></h4>
                <p class='pad_bot2'>
                   <strong><?php echo $row['title'];?></strong>
                </p>
                <p class='pad_bot1'><?php echo $row['des'];?></p>
            </section>
           </div>
        <?php
           endwhile;
           else:
        ?>
        <p>No active news</p>    
       <?php
           endif;
       ?>
        </div>
     </div>
</article>
<!-- /content-->

Comments

0

// Numeric array

$row=mysqli_fetch_array($result,MYSQLI_NUM); 

// Associative array

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

So for your code, you need to change:

$rows = mysqli_fetch_array($result);

to:

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

Also, you should check for number of rows before fetching by using:

$rowcount=mysqli_num_rows($result);

Only if $rowcount is more than 0, you should mysqli_fetch_array.

1 Comment

Hope you aren't making any typos with $row and $rows, Also, echo the query, execute it in your MySQL client and check if it really has some output.

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.