My code should print out all the users' usernames it finds, but it does not, merely prints 'User Found:' once. Please help me fix it; this is my code, thanks!
<?php
echo'
<form method="post">
<input name="newSearch" id="newSearch" class="inputs" placeholder="Search for a user...">
<input type="submit" id="submit_search" name="submit_search" class="button" value="Search">
</form>';
if ($_POST['submit_search']) {
$search = $_POST['newSearch'];
$getUser = mysql_query("SELECT * FROM Users WHERE Username LIKE '%$search%'");
while($id=mysql_fetch_assoc($getUser)){
$gU = mysql_fetch_object($getUser);
echo "User Found: ", $gU->Username, "<br />";
}
}
?>
Try any of the below,
while($id=mysql_fetch_assoc($getUser)){
echo "User Found: ". $id["Username"]. "<br />";
}
OR,
while($gU = mysql_fetch_object($getUser)){
echo "User Found: ". $gU->Username . "<br />";
}
Anyone of the above code helps you to survive your situation. :)
echo "User Found: " . $gU->Username . "<br />"
Should Be:
echo "User Found: " . $id["Username"] . "<br />"
EDIT:
You should choose to work with arrays or objects, please view: http://stackoverflow.com/questions/1536813/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-object
for the differences. I would also advise you to move away from depreciated code.
Work with Object:
while($id=mysql_fetch_object($getUser)){
echo "User Found: " . $id->Username . "<br />";
}
Work with Array:
while($id=mysql_fetch_assoc($getUser)){
echo "User Found: " . $id["username"] . "<br />";
}
You're going through two records with each iteration of the loop. mysql_fetch_assoc
and mysql_fetch_object
each grab a record and increment the pointer into the record set. You almost certainly want.
You almost certainly want:
while($gU=mysql_fetch_object($getUser)){
echo "User Found: " . $gU->Username . "<br />";
}
or
while($gU=mysql_fetch_assocc($getUser)){
echo "User Found: " . $gU["Username"] . "<br />";
}