dean7 Posted September 7, 2016 Share Posted September 7, 2016 Hey guys, I'm attempting to code a simple find user script for my website in which I cannot get my head around why my data in my while loop isn't displaying. I keep having breaks from coding as I only do it for something todo when I have time so this website wont be going live its just for me to make so if its unsecure its no big deal. if (isset($_POST['user_button']) && ($_POST['theusername'])){ $theusername = $_POST['theusername']; $query11 = "SELECT * FROM UsersRegTable WHERE username LIKE ':theusername'";$select = $db->prepare($query11);$select->bindValue(":theusername", "%".$theusername."%"); $select->execute();$total = count($select); echo $total; // always finds 1 echo "</table><br /><table width='25%' border='1' align='center' cellpadding='0' cellspacing='0' class='tableborder'><tr><td colspan='2' class='header' align='center'>Users:</td></tr><tr><td class='forum' align='center'>Username:</td><td class='forum' align='center'>Rank:</td></tr>"; if ($total == "0"){echo "<tr> <td colspan='2' align='center'>Username was not found!</td></tr>";} while($Ob = $select->fetch(PDO::FETCH_ASSOC)){ echo "<tr> <td align='center'><a href='profile.php?view=".$Ob['username']."'>".$Ob['username']."</a></td><td align='center'>".$Ob['rank']."</td></tr> </tr>"; } } Back on subject, when I echo the rows its found it always says 1 although it never displays the usernames it has found in the loop? Any help will be great! Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/302097-displaying-data-in-while-loop/ Share on other sites More sharing options...
Jacques1 Posted September 7, 2016 Share Posted September 7, 2016 You cannot count() a PDOStatement. The “1” you're seeing is a nonsense value which is returned by default when there's nothing to be counted. Simply fetch() from the statement and check if you get a row or false. Quote Link to comment https://forums.phpfreaks.com/topic/302097-displaying-data-in-while-loop/#findComment-1537141 Share on other sites More sharing options...
dean7 Posted September 7, 2016 Author Share Posted September 7, 2016 You cannot count() a PDOStatement. The “1” you're seeing is a nonsense value which is returned by default when there's nothing to be counted. Simply fetch() from the statement and check if you get a row or false. I've actually removed the count() part to see if the while loop data was showing but even without it its still not showing Quote Link to comment https://forums.phpfreaks.com/topic/302097-displaying-data-in-while-loop/#findComment-1537143 Share on other sites More sharing options...
Jacques1 Posted September 7, 2016 Share Posted September 7, 2016 That's the whole point I'm trying to make: Your query doesn't yield any rows. The reason is that you're literally searching for the username :theusername Leave out the quotes if you want to search with a placeholder: "SELECT * FROM UsersRegTable WHERE username LIKE :theusername" Quote Link to comment https://forums.phpfreaks.com/topic/302097-displaying-data-in-while-loop/#findComment-1537144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.