Use PHP variable in a SELECT LIKE query












0















I'm trying to execute a mysqli query using the following code:



$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:



$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


it gives me zero results.



I have tried also several LIKE formats such ...('".$var."%')"; or CONCAT($var, '%'), but nothing.



How can get the same results as the first query usin a variable like in the sencond query?



The variable is get by a query which will select all description items and then, inside a while loop, it will look for the first Capital letters of each item:



$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);


Each row has values like "AB CD EF something else in not capital letters"



Thank you.










share|improve this question

























  • how about $var = 'AB CD %'; ?

    – Always Sunny
    Nov 24 '18 at 16:28











  • Yes, tried, also.

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Maybe is important to refer how I get the variable. I will edit the post

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Yes that is very much important. I guess you need to escape the string also.

    – Always Sunny
    Nov 24 '18 at 16:32








  • 2





    How about using prepared statements...

    – dn Fer
    Nov 24 '18 at 16:35
















0















I'm trying to execute a mysqli query using the following code:



$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:



$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


it gives me zero results.



I have tried also several LIKE formats such ...('".$var."%')"; or CONCAT($var, '%'), but nothing.



How can get the same results as the first query usin a variable like in the sencond query?



The variable is get by a query which will select all description items and then, inside a while loop, it will look for the first Capital letters of each item:



$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);


Each row has values like "AB CD EF something else in not capital letters"



Thank you.










share|improve this question

























  • how about $var = 'AB CD %'; ?

    – Always Sunny
    Nov 24 '18 at 16:28











  • Yes, tried, also.

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Maybe is important to refer how I get the variable. I will edit the post

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Yes that is very much important. I guess you need to escape the string also.

    – Always Sunny
    Nov 24 '18 at 16:32








  • 2





    How about using prepared statements...

    – dn Fer
    Nov 24 '18 at 16:35














0












0








0








I'm trying to execute a mysqli query using the following code:



$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:



$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


it gives me zero results.



I have tried also several LIKE formats such ...('".$var."%')"; or CONCAT($var, '%'), but nothing.



How can get the same results as the first query usin a variable like in the sencond query?



The variable is get by a query which will select all description items and then, inside a while loop, it will look for the first Capital letters of each item:



$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);


Each row has values like "AB CD EF something else in not capital letters"



Thank you.










share|improve this question
















I'm trying to execute a mysqli query using the following code:



$sql = "SELECT * FROM `table` WHERE `description` LIKE ('AB CD %')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


and this query gives me 6 results. It will only looks for items like "AB CD EF..." and not items like "AB CDEF...", which is exactly what I want.
But if I give to the LIKE value a variable like this:



$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE ('$var%')";
$result= mysqli_query($con, $sql) or die(mysqli_error());


it gives me zero results.



I have tried also several LIKE formats such ...('".$var."%')"; or CONCAT($var, '%'), but nothing.



How can get the same results as the first query usin a variable like in the sencond query?



The variable is get by a query which will select all description items and then, inside a while loop, it will look for the first Capital letters of each item:



$name = $row['description'];
$expr = '/[A-Z]*/';
preg_match_all($expr, $name, $res);
$var = implode(' ', $res[0]);


Each row has values like "AB CD EF something else in not capital letters"



Thank you.







php mysqli






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 16:39







Goncalorsr

















asked Nov 24 '18 at 16:26









GoncalorsrGoncalorsr

134




134













  • how about $var = 'AB CD %'; ?

    – Always Sunny
    Nov 24 '18 at 16:28











  • Yes, tried, also.

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Maybe is important to refer how I get the variable. I will edit the post

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Yes that is very much important. I guess you need to escape the string also.

    – Always Sunny
    Nov 24 '18 at 16:32








  • 2





    How about using prepared statements...

    – dn Fer
    Nov 24 '18 at 16:35



















  • how about $var = 'AB CD %'; ?

    – Always Sunny
    Nov 24 '18 at 16:28











  • Yes, tried, also.

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Maybe is important to refer how I get the variable. I will edit the post

    – Goncalorsr
    Nov 24 '18 at 16:31











  • Yes that is very much important. I guess you need to escape the string also.

    – Always Sunny
    Nov 24 '18 at 16:32








  • 2





    How about using prepared statements...

    – dn Fer
    Nov 24 '18 at 16:35

















how about $var = 'AB CD %'; ?

– Always Sunny
Nov 24 '18 at 16:28





how about $var = 'AB CD %'; ?

– Always Sunny
Nov 24 '18 at 16:28













Yes, tried, also.

– Goncalorsr
Nov 24 '18 at 16:31





Yes, tried, also.

– Goncalorsr
Nov 24 '18 at 16:31













Maybe is important to refer how I get the variable. I will edit the post

– Goncalorsr
Nov 24 '18 at 16:31





Maybe is important to refer how I get the variable. I will edit the post

– Goncalorsr
Nov 24 '18 at 16:31













Yes that is very much important. I guess you need to escape the string also.

– Always Sunny
Nov 24 '18 at 16:32







Yes that is very much important. I guess you need to escape the string also.

– Always Sunny
Nov 24 '18 at 16:32






2




2





How about using prepared statements...

– dn Fer
Nov 24 '18 at 16:35





How about using prepared statements...

– dn Fer
Nov 24 '18 at 16:35












3 Answers
3






active

oldest

votes


















1














How about this after wrapping the variable with curly braces {}? Also I just removed the extra parenthesis () after LIKE :)



$var = "AB CD ";
$sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";





share|improve this answer
























  • Yes I tried this, also.

    – Goncalorsr
    Nov 24 '18 at 16:39



















0














Well, user3783243 found the problem.
When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
Thank you all.






share|improve this answer































    -1














    Use :



    $var = "AB CD ";
    $sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";


    But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php






    share|improve this answer
























    • That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

      – user3783243
      Nov 24 '18 at 18:06











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460128%2fuse-php-variable-in-a-select-like-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    How about this after wrapping the variable with curly braces {}? Also I just removed the extra parenthesis () after LIKE :)



    $var = "AB CD ";
    $sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";





    share|improve this answer
























    • Yes I tried this, also.

      – Goncalorsr
      Nov 24 '18 at 16:39
















    1














    How about this after wrapping the variable with curly braces {}? Also I just removed the extra parenthesis () after LIKE :)



    $var = "AB CD ";
    $sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";





    share|improve this answer
























    • Yes I tried this, also.

      – Goncalorsr
      Nov 24 '18 at 16:39














    1












    1








    1







    How about this after wrapping the variable with curly braces {}? Also I just removed the extra parenthesis () after LIKE :)



    $var = "AB CD ";
    $sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";





    share|improve this answer













    How about this after wrapping the variable with curly braces {}? Also I just removed the extra parenthesis () after LIKE :)



    $var = "AB CD ";
    $sql = "SELECT * FROM `table` WHERE `description` LIKE '{$var}%'";






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 16:31









    Always SunnyAlways Sunny

    16.4k32847




    16.4k32847













    • Yes I tried this, also.

      – Goncalorsr
      Nov 24 '18 at 16:39



















    • Yes I tried this, also.

      – Goncalorsr
      Nov 24 '18 at 16:39

















    Yes I tried this, also.

    – Goncalorsr
    Nov 24 '18 at 16:39





    Yes I tried this, also.

    – Goncalorsr
    Nov 24 '18 at 16:39













    0














    Well, user3783243 found the problem.
    When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
    Thank you all.






    share|improve this answer




























      0














      Well, user3783243 found the problem.
      When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
      Thank you all.






      share|improve this answer


























        0












        0








        0







        Well, user3783243 found the problem.
        When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
        Thank you all.






        share|improve this answer













        Well, user3783243 found the problem.
        When I implode the array to get the $var in a string, for some reason I cannot explain, the array elements (a lot of them) that had no value were included. The solution was to explode the $var, pass it through the array_filter function, to delete the empty elements and implode it again.
        Thank you all.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 18:22









        GoncalorsrGoncalorsr

        134




        134























            -1














            Use :



            $var = "AB CD ";
            $sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";


            But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php






            share|improve this answer
























            • That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

              – user3783243
              Nov 24 '18 at 18:06
















            -1














            Use :



            $var = "AB CD ";
            $sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";


            But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php






            share|improve this answer
























            • That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

              – user3783243
              Nov 24 '18 at 18:06














            -1












            -1








            -1







            Use :



            $var = "AB CD ";
            $sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";


            But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php






            share|improve this answer













            Use :



            $var = "AB CD ";
            $sql = "SELECT * FROM `table` WHERE `description` LIKE '".$var."%'";


            But this is a bad practice as well as highly vulnerable, use prepared statement instead : http://php.net/manual/en/book.pdo.php







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 17:57









            LabLab

            1,00489




            1,00489













            • That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

              – user3783243
              Nov 24 '18 at 18:06



















            • That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

              – user3783243
              Nov 24 '18 at 18:06

















            That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

            – user3783243
            Nov 24 '18 at 18:06





            That is the same as what the OP has. PDO can be just as secure/insecure as mysqli.

            – user3783243
            Nov 24 '18 at 18:06


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460128%2fuse-php-variable-in-a-select-like-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin