phpmysqldatabaseoperatorsquery-string

MySql search query fails to return rows containing %20 inside


I have a problem with my database queries. When I try to pull rows where spaces were converted to %20 earlier, the query returns null. I have tested other rows which works fine, why is this problem occurring? Do I need to remove the %20's manually from each row to get it to working?

Thanks!

database screenshot

<?php

include_once 'includes/db_connect.php';

$query = "SELECT * FROM USERS WHERE LIKES= '$_GET[imgname]' ";

//Creating resonse json array, with another array inside
$jsonResponse = array( "info" =>array() );

if($result = mysqli_query($mysqli, $query)) {
    while($row = mysqli_fetch_assoc($result)){

     $jsonRow = array(        

         'names'            =>      $row['USERNAME'],
         'userIds'          =>      $row['USERID']   

        );          

        //adding the $jsonRow array to the end of the "users" array as key/value
        array_push($jsonResponse["info"], $jsonRow);
    }   

}
    //encoding to json for the app
    echo json_encode($jsonResponse);



?>

Solution

  • Change your SQL query to:

    $query = "SELECT * FROM USERS WHERE LIKES= '".rawurlencode($_GET[imgname])."' ";
    

    The LIKES column has image path encoded (space converted to %20). Therefore, $_GET[imgname] should be encoded to match the database record.