mysql - Repeating Results PHP Query While Loop -
i'm retrieving data mysql ajax , php without refreshing , issue results displayed duplicate . want 1 instance of username display .
the table contains username has unique index set not allow duplicate names (i think) while statement believe causing results duplicate
how can please ? correct display once instance of each username . sorry in advance don't know hopeful can .
<?php include('..\db.php'); $con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname); $q1 = mysqli_query($con,"select * tbl1 username"); $data=""; // if search true if(isset($_post['search'])) { // $var = $_post['search']; if($query = mysqli_query($con,"select username fromtbl1 username '%$var%'")) { // possible creating duplicate results while($row = mysqli_fetch_array($query)) { $data .= $data . '<div>' . $row['username'] . '</div>'; } echo $data; } }else{ } ?> <html> <head> <script src="https://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrplz+8vdxt2fne1zvzxckccebi/c8dt5xyaqbjxqio=" crossorigin="anonymous"></script> <script> $(function(){ $('.input').keyup(function(){ var = $('.input').val(); $.post('livesusers.php',{"search":a},function(data){ $('#display').html(data); }); }); }); </script> </head> <body> // form input text , search <form action= "livesusers.php" method='post'> <input type="text" name="search" class='input'> </form> <div id='display' style='margin-top:100px'></div> </body>
the error in while loop said. concatenating twice on each cicle.
try replace this:
// possible creating duplicate results while($row = mysqli_fetch_array($query)) { $data .= $data . '<div>' . $row['username'] . '</div>'; } echo $data;
to this:
while($row = mysqli_fetch_array($query)) { $data .= '<div>' . $row['username'] . '</div>'; } echo $data;
Comments
Post a Comment