I have a little problem with my select query. If I input a string of numbers (int) and the user with that "username" exists, I get a result named $resultw
. But if I enter in the form a string with letters (varchar) and a user have this username in my database it didnt works and I didnt get any (error) message. What could happen that?
my php:
if(isset($_POST["submit"])){
$hostname='localhost';
$user='root';
$password='';
$uinput = $_POST['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=game",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id, email, username
FROM user
WHERE username = $uinput"; //
if ($resi = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$resultw = $resi->fetchAll();
}
if($resultw >= 0) {
//do something
}
else {
echo "The user you are searching for does not exist.\nPlease check your input.";
}
}
catch(PDOException $e) {
//
}
}
Dont know if its needed but thats my form:
<form style="width:100%" data-ajax="false" action="./username.php" method="post">
<div style="width:100%" class="ui-grid-a">
<div style="width:75%;padding-left:5%" class="ui-block-a">
<div>
<input data-inline="true" type="text" name="username" id="username"/></div></div>
<div style="width:20%" class="ui-block-b"><div>
<input data-inline="true" type="submit" name="submit" value="Go" class="ui-btn"/>
</div></div>
</div>
</form>
my foreach loop:
<?php foreach ($resultw as $keyres => $rowres): ?>
<li>
<a href="ime.php"><?php echo $rowres['username']; ?></a>
</li>
<?php endforeach; ?>