Home PHP while command missing first entry
Reply: 2

PHP while command missing first entry

Steve Mack
1#
Steve Mack Published in 2017-12-07 18:19:04Z

I am trying to use the while loop to print multiple entries from a MySQL database. Everywhere I do this it is missing the first item on the list and only manages to print from the second one on.

<?php
require 'db.php';
$udate = $mysqli->escape_string($_POST['date']);
$result = $mysqli->query("SELECT * FROM videos WHERE date='$udate'");
$user = $result->fetch_assoc();
$_SESSION['file_name'] = $user['file_name'];
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>View</title>
        <link rel="stylesheet" type="text/css" href="css/nominatorstyle.css">  
    </head>
    <body>
        <div class="header">
            <h2>Results.</h2>
            <form action="watch.php" method='POST'><table>
                <?php 
                    if($result->num_rows > 0){
                        while($row = $result->fetch_assoc()){
                            $vid = $row['vid'];                           
                            $source = "uploads/".$row['file_name'];
                            echo "<tr><td>".$row['vid']."</td>
<td>".$row['file_name']."</td>";
                            echo "<td><a href=watch.php?
source=$source>Play</a></td></tr>";
                        }
                    }else{
                    echo "0 results";
                    } 
                ?>
                </table></form>
            <a href="profile.php"><button class="button-block">Home</button></a>
        </div>      
    </body>

I'd like it to print all matching criteria. I'm getting only the entries after the first one.

Dimitar Danev
2#
Dimitar Danev Reply to 2017-12-07 19:18:09Z
        require 'db.php';
        $udate = $mysqli->escape_string($_POST['date']);
        $result = $mysqli->query("SELECT * FROM videos WHERE date='$udate'");

        /*
         This fetch_assoc will get your first row #0 of the query
         next fetch_assoc with your loop on the $result 
         will start as you said from row #1
        */
        $user = $result->fetch_assoc(); //your missing row #0
        $_SESSION['file_name'] = $user['file_name'];
Michael T
3#
Michael T Reply to 2017-12-07 19:02:08Z

You missed a "}" at the end of your while loop, that might be a problem.. I also changed your >=1 of your num_rows conditions to > 0. Try this:

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $vid = $row['vid'];                           
        $source = "uploads/" . $row['file_name'];
        $htmlRow  = "<tr>\n";
        $htmlRow .= "<td>" . $row['vid'] . "</td>\n";
        $htmlRow .= "<td>" . $row['file_name'] . "</td>\n";
        $htmlRow .= "<td>" . $row['vid'] . "</td>\n";
        $htmlRow .= "<td><a href=\"watch.php?source=" . $source . "\">Play</a></td>\n";
        $htmlRow .= "</tr>\n";
        echo $htmlRow;
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30379 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO