Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/302097-displaying-data-in-while-loop/
Share on other sites

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

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"
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.