Reputation:
I want to query my SQL database for a name. Let's say a certain table contains the column "names" and has two inputs "John Silva" and "Dave Silva". I want to make a query where I get all inputs with the name "Silva" and store them in a way that I can later echo out the result onto my HTML code. Here's what I got so far (keep in mind that it is not working, that's why i came here to ask :) ):
$query = "SELECT ID, email, fullname, permission FROM users WHERE name LIKE '$data'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$userName = $row['fullname'];
$userHashedPermission = $row['permission'];
$ID = $row['ID'];
$userEmail = $row['email'];
}
if(password_verify('sim', $userHashedPermission)){
$userPermission='sim';
}else{
$userPermission='nao';
}
$callbackObj->name = $userName;
$callbackObj->permission = $userPermission;
$callbackObj->Id = $ID;
$callbackObj->email = $userEmail;
$callbackJson = json_encode($callbackObj);
echo $callbackJson;
}else{
echo "something went wrong"
}
Upvotes: 1
Views: 181
Reputation: 2584
You need to make some changes in your query as
$query = "SELECT ID, email, fullname, permission FROM users WHERE name LIKE '%".$data."%'";
As per the comments
Do not use while
loop keep your code as
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_assoc($result));
$callbackJson = json_encode($row);
echo $callbackJson;
}
Upvotes: 0
Reputation: 1
I think your sample code and the introduction are totally explaining different thing.
From your introduction, I think you are trying to get all search results with the same surname and then save them in another different way.
If that is your point, we will have better way to sort it out.
Upvotes: 0
Reputation: 1269493
The functionality you want is like
. More importantly: learn to use parameters.
$query = "SELECT ID, email, fullname, permission FROM users WHERE name LIKE CONCAT('%', ?)";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "s", $data)
$result = mysqli_stmt_execute($conn, $query);
Upvotes: 1