How do I use 'N' in my prepared SELECT statement

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I'm connecting to an SQL Server 2008 database using pdo and the ODBC Driver 13 for SQL Server.



I'm trying to run the following query which searches the database for Chinese characters:



SELECT TOP (10) ... WHERE (sItemName LIKE N:term1)

$text = "%简况%";
$query = $this->DBH->prepare($sql);
$query->bindParam(':term1', $text, PDO::PARAM_STR );
$query->execute();


This query returns results with random characters so it looks like the Character encoding is somehow incorrect.



I was able to return the same results by running this query directly on the DB using Heidi:



SELECT TOP (10) ... WHERE (sItemName LIKE '%简况%')


Changing the query to the following by adding an 'N' before the string returned the correct results:



SELECT TOP (10) ... WHERE (sItemName LIKE N'%简况%')


But when I try to do the same with my prepared statement as shown below I get a syntax error:



SELECT TOP (10) ... WHERE (CI.sItemName LIKE N:term1)

$text = "%简况%";
$query = $this->DBH->prepare($sql);
$query->bindParam(':term1', $text, PDO::PARAM_STR );
$query->execute();


Here's the error:




Syntax error or access violation: 102 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'N:'.




So my question is how do I use 'N' in my prepared statement without getting an error. I'm not a DBO by trade so apologies if I've left anything out!



This returns the expected result:



$seach_term = (string)'简况';
$text = '%'.$seach_term.'%';
$query = $this->DBH->prepare("SELECT * FROM Articles WHERE sHeadline LIKE :term");
$query->bindParam(':term', $text, PDO::PARAM_STR);


But using the PARAM_STR_NATL constant doesn't return anything:



$query->bindParam(':term', $text, PDO::PARAM_STR_NATL);









share|improve this question









New contributor




Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    2
    down vote

    favorite












    I'm connecting to an SQL Server 2008 database using pdo and the ODBC Driver 13 for SQL Server.



    I'm trying to run the following query which searches the database for Chinese characters:



    SELECT TOP (10) ... WHERE (sItemName LIKE N:term1)

    $text = "%简况%";
    $query = $this->DBH->prepare($sql);
    $query->bindParam(':term1', $text, PDO::PARAM_STR );
    $query->execute();


    This query returns results with random characters so it looks like the Character encoding is somehow incorrect.



    I was able to return the same results by running this query directly on the DB using Heidi:



    SELECT TOP (10) ... WHERE (sItemName LIKE '%简况%')


    Changing the query to the following by adding an 'N' before the string returned the correct results:



    SELECT TOP (10) ... WHERE (sItemName LIKE N'%简况%')


    But when I try to do the same with my prepared statement as shown below I get a syntax error:



    SELECT TOP (10) ... WHERE (CI.sItemName LIKE N:term1)

    $text = "%简况%";
    $query = $this->DBH->prepare($sql);
    $query->bindParam(':term1', $text, PDO::PARAM_STR );
    $query->execute();


    Here's the error:




    Syntax error or access violation: 102 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'N:'.




    So my question is how do I use 'N' in my prepared statement without getting an error. I'm not a DBO by trade so apologies if I've left anything out!



    This returns the expected result:



    $seach_term = (string)'简况';
    $text = '%'.$seach_term.'%';
    $query = $this->DBH->prepare("SELECT * FROM Articles WHERE sHeadline LIKE :term");
    $query->bindParam(':term', $text, PDO::PARAM_STR);


    But using the PARAM_STR_NATL constant doesn't return anything:



    $query->bindParam(':term', $text, PDO::PARAM_STR_NATL);









    share|improve this question









    New contributor




    Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm connecting to an SQL Server 2008 database using pdo and the ODBC Driver 13 for SQL Server.



      I'm trying to run the following query which searches the database for Chinese characters:



      SELECT TOP (10) ... WHERE (sItemName LIKE N:term1)

      $text = "%简况%";
      $query = $this->DBH->prepare($sql);
      $query->bindParam(':term1', $text, PDO::PARAM_STR );
      $query->execute();


      This query returns results with random characters so it looks like the Character encoding is somehow incorrect.



      I was able to return the same results by running this query directly on the DB using Heidi:



      SELECT TOP (10) ... WHERE (sItemName LIKE '%简况%')


      Changing the query to the following by adding an 'N' before the string returned the correct results:



      SELECT TOP (10) ... WHERE (sItemName LIKE N'%简况%')


      But when I try to do the same with my prepared statement as shown below I get a syntax error:



      SELECT TOP (10) ... WHERE (CI.sItemName LIKE N:term1)

      $text = "%简况%";
      $query = $this->DBH->prepare($sql);
      $query->bindParam(':term1', $text, PDO::PARAM_STR );
      $query->execute();


      Here's the error:




      Syntax error or access violation: 102 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'N:'.




      So my question is how do I use 'N' in my prepared statement without getting an error. I'm not a DBO by trade so apologies if I've left anything out!



      This returns the expected result:



      $seach_term = (string)'简况';
      $text = '%'.$seach_term.'%';
      $query = $this->DBH->prepare("SELECT * FROM Articles WHERE sHeadline LIKE :term");
      $query->bindParam(':term', $text, PDO::PARAM_STR);


      But using the PARAM_STR_NATL constant doesn't return anything:



      $query->bindParam(':term', $text, PDO::PARAM_STR_NATL);









      share|improve this question









      New contributor




      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm connecting to an SQL Server 2008 database using pdo and the ODBC Driver 13 for SQL Server.



      I'm trying to run the following query which searches the database for Chinese characters:



      SELECT TOP (10) ... WHERE (sItemName LIKE N:term1)

      $text = "%简况%";
      $query = $this->DBH->prepare($sql);
      $query->bindParam(':term1', $text, PDO::PARAM_STR );
      $query->execute();


      This query returns results with random characters so it looks like the Character encoding is somehow incorrect.



      I was able to return the same results by running this query directly on the DB using Heidi:



      SELECT TOP (10) ... WHERE (sItemName LIKE '%简况%')


      Changing the query to the following by adding an 'N' before the string returned the correct results:



      SELECT TOP (10) ... WHERE (sItemName LIKE N'%简况%')


      But when I try to do the same with my prepared statement as shown below I get a syntax error:



      SELECT TOP (10) ... WHERE (CI.sItemName LIKE N:term1)

      $text = "%简况%";
      $query = $this->DBH->prepare($sql);
      $query->bindParam(':term1', $text, PDO::PARAM_STR );
      $query->execute();


      Here's the error:




      Syntax error or access violation: 102 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'N:'.




      So my question is how do I use 'N' in my prepared statement without getting an error. I'm not a DBO by trade so apologies if I've left anything out!



      This returns the expected result:



      $seach_term = (string)'简况';
      $text = '%'.$seach_term.'%';
      $query = $this->DBH->prepare("SELECT * FROM Articles WHERE sHeadline LIKE :term");
      $query->bindParam(':term', $text, PDO::PARAM_STR);


      But using the PARAM_STR_NATL constant doesn't return anything:



      $query->bindParam(':term', $text, PDO::PARAM_STR_NATL);






      sql-server sql-server-2008 php odbc character-set






      share|improve this question









      New contributor




      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 18 hours ago





















      New contributor




      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      Los Porcos

      112




      112




      New contributor




      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Los Porcos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          You should bind them using the (relatively new) PDO_PARAM_STR_NATL constant. This constant is available in PHP 7.2 and up. It will cause the desired N prefix to be added.



          $query->bindParam(':term1', $text, PDO::PDO_PARAM_STR_NATL);


          In case you're interested, some history can be found here:
          https://bugs.php.net/bug.php?id=60818



          The Github commit that is linked there shows how N is added (lines 178-184):
          https://github.com/php/php-src/commit/4afce8ec8c6660ebd9f9eb174d2614361d1c6129






          share|improve this answer




















          • Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
            – Los Porcos
            21 hours ago











          • Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
            – Los Porcos
            20 hours ago

















          up vote
          1
          down vote













          The N is only needed for literals. In a prepared statement you bind typed parameters to parameter placeholders. The parameters will be typed as NVarchar and set to Unicode values.






          share|improve this answer




















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "182"
            ;
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            );



            );






            Los Porcos is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f222441%2fhow-do-i-use-n-in-my-prepared-select-statement%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            You should bind them using the (relatively new) PDO_PARAM_STR_NATL constant. This constant is available in PHP 7.2 and up. It will cause the desired N prefix to be added.



            $query->bindParam(':term1', $text, PDO::PDO_PARAM_STR_NATL);


            In case you're interested, some history can be found here:
            https://bugs.php.net/bug.php?id=60818



            The Github commit that is linked there shows how N is added (lines 178-184):
            https://github.com/php/php-src/commit/4afce8ec8c6660ebd9f9eb174d2614361d1c6129






            share|improve this answer




















            • Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
              – Los Porcos
              21 hours ago











            • Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
              – Los Porcos
              20 hours ago














            up vote
            2
            down vote













            You should bind them using the (relatively new) PDO_PARAM_STR_NATL constant. This constant is available in PHP 7.2 and up. It will cause the desired N prefix to be added.



            $query->bindParam(':term1', $text, PDO::PDO_PARAM_STR_NATL);


            In case you're interested, some history can be found here:
            https://bugs.php.net/bug.php?id=60818



            The Github commit that is linked there shows how N is added (lines 178-184):
            https://github.com/php/php-src/commit/4afce8ec8c6660ebd9f9eb174d2614361d1c6129






            share|improve this answer




















            • Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
              – Los Porcos
              21 hours ago











            • Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
              – Los Porcos
              20 hours ago












            up vote
            2
            down vote










            up vote
            2
            down vote









            You should bind them using the (relatively new) PDO_PARAM_STR_NATL constant. This constant is available in PHP 7.2 and up. It will cause the desired N prefix to be added.



            $query->bindParam(':term1', $text, PDO::PDO_PARAM_STR_NATL);


            In case you're interested, some history can be found here:
            https://bugs.php.net/bug.php?id=60818



            The Github commit that is linked there shows how N is added (lines 178-184):
            https://github.com/php/php-src/commit/4afce8ec8c6660ebd9f9eb174d2614361d1c6129






            share|improve this answer












            You should bind them using the (relatively new) PDO_PARAM_STR_NATL constant. This constant is available in PHP 7.2 and up. It will cause the desired N prefix to be added.



            $query->bindParam(':term1', $text, PDO::PDO_PARAM_STR_NATL);


            In case you're interested, some history can be found here:
            https://bugs.php.net/bug.php?id=60818



            The Github commit that is linked there shows how N is added (lines 178-184):
            https://github.com/php/php-src/commit/4afce8ec8c6660ebd9f9eb174d2614361d1c6129







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Peter B

            209110




            209110











            • Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
              – Los Porcos
              21 hours ago











            • Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
              – Los Porcos
              20 hours ago
















            • Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
              – Los Porcos
              21 hours ago











            • Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
              – Los Porcos
              20 hours ago















            Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
            – Los Porcos
            21 hours ago





            Strange, I get the following error message when using this constant. PHP Fatal error: Uncaught Error: Undefined class constant 'PDO_PARAM_STR_NATL' I'm on PHP72. Will do some more digging. Thanks
            – Los Porcos
            21 hours ago













            Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
            – Los Porcos
            20 hours ago




            Using PARAM_STR_NATL doesn't give me an error but I get no results with this contant.
            – Los Porcos
            20 hours ago












            up vote
            1
            down vote













            The N is only needed for literals. In a prepared statement you bind typed parameters to parameter placeholders. The parameters will be typed as NVarchar and set to Unicode values.






            share|improve this answer
























              up vote
              1
              down vote













              The N is only needed for literals. In a prepared statement you bind typed parameters to parameter placeholders. The parameters will be typed as NVarchar and set to Unicode values.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                The N is only needed for literals. In a prepared statement you bind typed parameters to parameter placeholders. The parameters will be typed as NVarchar and set to Unicode values.






                share|improve this answer












                The N is only needed for literals. In a prepared statement you bind typed parameters to parameter placeholders. The parameters will be typed as NVarchar and set to Unicode values.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                David Browne - Microsoft

                9,676725




                9,676725




















                    Los Porcos is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Los Porcos is a new contributor. Be nice, and check out our Code of Conduct.












                    Los Porcos is a new contributor. Be nice, and check out our Code of Conduct.











                    Los Porcos is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f222441%2fhow-do-i-use-n-in-my-prepared-select-statement%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay