MySQL table names in PHP code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MikeB

    #1

    MySQL table names in PHP code

    I'm learning PHP and MySQL. In the samples I work with, the SQL table
    names are "escaped" in accent grave (`) marks, not in single or double
    quotes.

    Can anyone explain that?

    For instance, I have the following:

    $sql_command = "SELECT * from `7-1_List`";

    I tried looking around on the php.net site, but clearly I'm not using
    the right search terms.

    While I'm asking, I think I've figured out how double quotes (")
    differ from single quotes ('), but if there is a discussion that
    someone can point me to, that would be very nice.

    Thanks.
  • Jerry Stuckle

    #2
    Re: MySQL table names in PHP code

    MikeB wrote:
    I'm learning PHP and MySQL. In the samples I work with, the SQL table
    names are "escaped" in accent grave (`) marks, not in single or double
    quotes.
    >
    Can anyone explain that?
    >
    For instance, I have the following:
    >
    $sql_command = "SELECT * from `7-1_List`";
    >
    I tried looking around on the php.net site, but clearly I'm not using
    the right search terms.
    >
    While I'm asking, I think I've figured out how double quotes (")
    differ from single quotes ('), but if there is a discussion that
    someone can point me to, that would be very nice.
    >
    Thanks.
    That's because it's a MySQL convention and has nothing to do with PHP.
    Try the MySQL documentation.

    SQL commands are just strings PHP passes to the database. From there
    on, it's the databases responsibility.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • sheldonlg

      #3
      Re: MySQL table names in PHP code

      Jerry Stuckle wrote:
      MikeB wrote:
      >I'm learning PHP and MySQL. In the samples I work with, the SQL table
      >names are "escaped" in accent grave (`) marks, not in single or double
      >quotes.
      >>
      >Can anyone explain that?
      >>
      >For instance, I have the following:
      >>
      >$sql_command = "SELECT * from `7-1_List`";
      >>
      >I tried looking around on the php.net site, but clearly I'm not using
      >the right search terms.
      >>
      >While I'm asking, I think I've figured out how double quotes (")
      >differ from single quotes ('), but if there is a discussion that
      >someone can point me to, that would be very nice.
      >>
      >Thanks.
      >
      That's because it's a MySQL convention and has nothing to do with PHP.
      Try the MySQL documentation.
      >
      SQL commands are just strings PHP passes to the database. From there
      on, it's the databases responsibility.
      >
      They don't have to be escaped. They work fine without them.

      $sql_command = "SELECT * from foo";

      works.

      Comment

      • Curtis

        #4
        Re: MySQL table names in PHP code

        MikeB wrote:
        I'm learning PHP and MySQL. In the samples I work with, the SQL table
        names are "escaped" in accent grave (`) marks, not in single or double
        quotes.
        >
        Can anyone explain that?
        >
        For instance, I have the following:
        >
        $sql_command = "SELECT * from `7-1_List`";
        >
        I tried looking around on the php.net site, but clearly I'm not using
        the right search terms.
        >
        While I'm asking, I think I've figured out how double quotes (")
        differ from single quotes ('), but if there is a discussion that
        someone can point me to, that would be very nice.
        >
        Thanks.
        You might want to try following up in comp.databases. mysql.

        --
        Curtis

        Comment

        • thelma@uwm.edu

          #5
          Re: MySQL table names in PHP code

          MikeB <MPBrede@gmail. comwrote:
          : I'm learning PHP and MySQL. In the samples I work with, the SQL table
          : names are "escaped" in accent grave (`) marks, not in single or double
          : quotes.

          : Can anyone explain that?

          : For instance, I have the following:

          : $sql_command = "SELECT * from `7-1_List`";

          I discovered backquotes when I inherited a MYSQL database that
          had a table with a column named 'interval'. MYSQL refused to let
          me reference that column: I couldn't select it, update it, remove
          it. The good people here and at the MYSQL newsgroup told me that
          I would need to add those backquotes because 'interval' is
          otherwise recognized as a MYSQL datatype.

          I would guess that the backquotes are hiding that '-' [minus?]
          from MYSQL--I prefer to rename things that need such extra handling.
          --thelma

          Comment

          • Chuck Anderson

            #6
            Re: MySQL table names in PHP code

            MikeB wrote:
            I'm learning PHP and MySQL. In the samples I work with, the SQL table
            names are "escaped" in accent grave (`) marks, not in single or double
            quotes.
            >
            Can anyone explain that?
            >
            For instance, I have the following:
            >
            $sql_command = "SELECT * from `7-1_List`";
            >
            I tried looking around on the php.net site, but clearly I'm not using
            the right search terms.
            >
            While I'm asking, I think I've figured out how double quotes (")
            differ from single quotes ('), but if there is a discussion that
            someone can point me to, that would be very nice.
            >
            Re: quotes - http://us.php.net/types.string

            --
            *************** **************
            Chuck Anderson • Boulder, CO

            Nothing he's got he really needs
            Twenty first century schizoid man.
            *************** *************** *****

            Comment

            • Tim Roberts

              #7
              Re: MySQL table names in PHP code

              sheldonlg <sheldonlgwrote :
              >Jerry Stuckle wrote:
              >MikeB wrote:
              >>I'm learning PHP and MySQL. In the samples I work with, the SQL table
              >>names are "escaped" in accent grave (`) marks, not in single or double
              >>quotes.
              >>>
              >>Can anyone explain that?
              >>>
              >>For instance, I have the following:
              >>>
              >>$sql_comman d = "SELECT * from `7-1_List`";
              >>>...
              >That's because it's a MySQL convention and has nothing to do with PHP.
              >Try the MySQL documentation.
              >>
              >SQL commands are just strings PHP passes to the database. From there
              >on, it's the databases responsibility.
              >
              >They don't have to be escaped. They work fine without them.
              >
              >$sql_command = "SELECT * from foo";
              >
              >works.
              Of course it does, but "foo" is a very different name from "7-1_List". His
              table name contains special characters that DO need to be escaped.
              --
              Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • Guillaume

                #8
                Re: MySQL table names in PHP code

                sheldonlg a écrit :
                They don't have to be escaped. They work fine without them.
                >
                $sql_command = "SELECT * from foo";
                >
                works.
                Yeah but this escaping is here to use named fields/tables with a name
                matching a MySQL reserved name.

                I encountered it a lot with int and (previously) date, those indeed
                being MySQL reserved types, but also a nice name for a date field and a
                (RPG game based site) intelligence status (just like str, hp, vit...).

                SELECT `int` FROM mytable
                works whereas
                SELECT int FROM mytable
                tries to access the int type and fails.

                Long ago (and as far as I remember), the same applied to date, referring
                to the date() function.

                Btw this is nowhere close to a PHP related issue ^^

                Regards,
                --
                Guillaume

                Comment

                • Jerry Stuckle

                  #9
                  Re: MySQL table names in PHP code

                  sheldonlg wrote:
                  Jerry Stuckle wrote:
                  >MikeB wrote:
                  >>I'm learning PHP and MySQL. In the samples I work with, the SQL table
                  >>names are "escaped" in accent grave (`) marks, not in single or double
                  >>quotes.
                  >>>
                  >>Can anyone explain that?
                  >>>
                  >>For instance, I have the following:
                  >>>
                  >>$sql_comman d = "SELECT * from `7-1_List`";
                  >>>
                  >>I tried looking around on the php.net site, but clearly I'm not using
                  >>the right search terms.
                  >>>
                  >>While I'm asking, I think I've figured out how double quotes (")
                  >>differ from single quotes ('), but if there is a discussion that
                  >>someone can point me to, that would be very nice.
                  >>>
                  >>Thanks.
                  >>
                  >That's because it's a MySQL convention and has nothing to do with PHP.
                  >Try the MySQL documentation.
                  >>
                  >SQL commands are just strings PHP passes to the database. From there
                  >on, it's the databases responsibility.
                  >>
                  >
                  They don't have to be escaped. They work fine without them.
                  >
                  $sql_command = "SELECT * from foo";
                  >
                  works.
                  It depends - sometimes you do. But that's a question for
                  comp.databases. mysql, not comp.lang.php.

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: MySQL table names in PHP code

                    Chuck Anderson wrote:
                    MikeB wrote:
                    >I'm learning PHP and MySQL. In the samples I work with, the SQL table
                    >names are "escaped" in accent grave (`) marks, not in single or double
                    >quotes.
                    >>
                    >Can anyone explain that?
                    >>
                    >For instance, I have the following:
                    >>
                    >$sql_command = "SELECT * from `7-1_List`";
                    >>
                    >I tried looking around on the php.net site, but clearly I'm not using
                    >the right search terms.
                    >>
                    >While I'm asking, I think I've figured out how double quotes (")
                    >differ from single quotes ('), but if there is a discussion that
                    >someone can point me to, that would be very nice.
                    >>
                    >
                    Re: quotes - http://us.php.net/types.string
                    >
                    Good try, but it has nothing to do with the user's question.

                    --
                    =============== ===
                    Remove the "x" from my email address
                    Jerry Stuckle
                    JDS Computer Training Corp.
                    jstucklex@attgl obal.net
                    =============== ===

                    Comment

                    • Chuck Anderson

                      #11
                      Re: MySQL table names in PHP code

                      Jerry Stuckle wrote:
                      Chuck Anderson wrote:
                      >
                      >MikeB wrote:
                      >>
                      >>I'm learning PHP and MySQL. In the samples I work with, the SQL table
                      >>names are "escaped" in accent grave (`) marks, not in single or double
                      >>quotes.
                      >>>
                      >>Can anyone explain that?
                      >>>
                      >>For instance, I have the following:
                      >>>
                      >>$sql_comman d = "SELECT * from `7-1_List`";
                      >>>
                      >>I tried looking around on the php.net site, but clearly I'm not using
                      >>the right search terms.
                      >>>
                      >>While I'm asking, I think I've figured out how double quotes (")
                      >>differ from single quotes ('), but if there is a discussion that
                      >>someone can point me to, that would be very nice.
                      >>>
                      >>>
                      >Re: quotes - http://us.php.net/types.string
                      >>
                      >>
                      >
                      Good try, but it has nothing to do with the user's question.
                      His question (to quote) about "how double quotes (") differ from single
                      quotes (')?"

                      It's spot on, Jerry. Did you even bother to look? That page it has a
                      very thorough discussion of Variable parsing and how to use both types.


                      --
                      *************** **************
                      Chuck Anderson • Boulder, CO

                      Nothing he's got he really needs
                      Twenty first century schizoid man.
                      *************** *************** *****

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: MySQL table names in PHP code

                        Chuck Anderson wrote:
                        Jerry Stuckle wrote:
                        >Chuck Anderson wrote:
                        >>
                        >>MikeB wrote:
                        >>>
                        >>>I'm learning PHP and MySQL. In the samples I work with, the SQL table
                        >>>names are "escaped" in accent grave (`) marks, not in single or double
                        >>>quotes.
                        >>>>
                        >>>Can anyone explain that?
                        >>>>
                        >>>For instance, I have the following:
                        >>>>
                        >>>$sql_comma nd = "SELECT * from `7-1_List`";
                        >>>>
                        >>>I tried looking around on the php.net site, but clearly I'm not using
                        >>>the right search terms.
                        >>>>
                        >>>While I'm asking, I think I've figured out how double quotes (")
                        >>>differ from single quotes ('), but if there is a discussion that
                        >>>someone can point me to, that would be very nice.
                        >>>>
                        >>Re: quotes - http://us.php.net/types.string
                        >>>
                        >>>
                        >>
                        >Good try, but it has nothing to do with the user's question.
                        >
                        His question (to quote) about "how double quotes (") differ from single
                        quotes (')?"
                        It's spot on, Jerry. Did you even bother to look? That page it has a
                        very thorough discussion of Variable parsing and how to use both types.
                        >
                        >
                        Read again. He was asking about back tickies (`) in MySQL statements,
                        not single quotes (') in PHP.

                        It had absolutely nothing to do with PHP strings.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        • NC

                          #13
                          Re: MySQL table names in PHP code

                          On May 27, 7:00 pm, MikeB <MPBr...@gmail. comwrote:
                          >
                          I'm learning PHP and MySQL. In the samples I work with, the SQL table
                          names are "escaped" in accent grave (`) marks, not in single or double
                          quotes.
                          Backticks (that's what they are called in programming, rather than
                          "accent graves") are identifier quotes in MySQL:

                          http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

                          Generally, they are not required, unless the identifier (the name of a
                          database, a table, a field, or another entity within a database)
                          happens to be the same as a reserved word or can be construed as an
                          expression. For example:

                          SELECT * FROM `select` WHERE id = 100;

                          In this case, you need to distinguish the table named `select` from
                          the beginning of the SELECT statement, so you have to use the
                          backticks.
                          For instance, I have the following:
                          >
                          $sql_command = "SELECT * from `7-1_List`";
                          Note that the name of the table includes a dash (aka the minus sign),
                          so if the name is not quoted, MySQL will be tempted to treat it as an
                          expression (7 minus 1_List), which, of course, is likely to produce an
                          SQL error.
                          I tried looking around on the php.net site
                          MySQL has a Web site, too, you know... :)
                          While I'm asking, I think I've figured out how double quotes (")
                          differ from single quotes ('), but if there is a discussion that
                          someone can point me to, that would be very nice.
                          Yes. See the PHP Manual (and read up on the heredoc syntax, too,
                          while you are on the subject of strings):



                          Cheers,
                          NC

                          Comment

                          • Chuck Anderson

                            #14
                            Re: MySQL table names in PHP code

                            Jerry Stuckle wrote:
                            Chuck Anderson wrote:
                            >
                            >Jerry Stuckle wrote:
                            >>
                            >>Chuck Anderson wrote:
                            >>>
                            >>>
                            >>>MikeB wrote:
                            >>>>
                            >>>>
                            >>>>I'm learning PHP and MySQL. In the samples I work with, the SQL table
                            >>>>names are "escaped" in accent grave (`) marks, not in single or double
                            >>>>quotes.
                            >>>>>
                            >>>>Can anyone explain that?
                            >>>>>
                            >>>>For instance, I have the following:
                            >>>>>
                            >>>>$sql_comman d = "SELECT * from `7-1_List`";
                            >>>>>
                            >>>>I tried looking around on the php.net site, but clearly I'm not using
                            >>>>the right search terms.
                            >>>>>
                            >>>>While I'm asking, I think I've figured out how double quotes (")
                            >>>>differ from single quotes ('), but if there is a discussion that
                            >>>>someone can point me to, that would be very nice.
                            >>>>>
                            >>>>>
                            >>>Re: quotes - http://us.php.net/types.string
                            >>>>
                            >>>>
                            >>>>
                            >>Good try, but it has nothing to do with the user's question.
                            >>>
                            >His question (to quote) about "how double quotes (") differ from single
                            >quotes (')?"
                            >It's spot on, Jerry. Did you even bother to look? That page it has a
                            >very thorough discussion of Variable parsing and how to use both types.
                            >>
                            >>
                            >B.S.
                            >
                            Read again. He was asking about back tickies (`) in MySQL statements,
                            not single quotes (') in PHP.
                            >
                            It had absolutely nothing to do with PHP strings.
                            Du-uh ...... try again He spoke of both. Here I'll quote it for you again:
                            I'm learning PHP and MySQL ......
                            ..... While I'm asking, I think I've figured out how double quotes (")
                            differ from single quotes ('), but if there is a discussion that
                            someone can point me to, that would be very nice.
                            Enough of this waste of time already. Haven't you better things to do?

                            Case closed.

                            --
                            *************** **************
                            Chuck Anderson • Boulder, CO

                            Nothing he's got he really needs
                            Twenty first century schizoid man.
                            *************** *************** *****

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: MySQL table names in PHP code

                              Chuck Anderson wrote:
                              Jerry Stuckle wrote:
                              >Chuck Anderson wrote:
                              >>
                              >>Jerry Stuckle wrote:
                              >>>
                              >>>Chuck Anderson wrote:
                              >>>>
                              >>>>
                              >>>>MikeB wrote:
                              >>>>>
                              >>>>>I'm learning PHP and MySQL. In the samples I work with, the SQL
                              >>>>>table
                              >>>>>names are "escaped" in accent grave (`) marks, not in single or
                              >>>>>double
                              >>>>>quotes.
                              >>>>>>
                              >>>>>Can anyone explain that?
                              >>>>>>
                              >>>>>For instance, I have the following:
                              >>>>>>
                              >>>>>$sql_comma nd = "SELECT * from `7-1_List`";
                              >>>>>>
                              >>>>>I tried looking around on the php.net site, but clearly I'm not using
                              >>>>>the right search terms.
                              >>>>>>
                              >>>>>While I'm asking, I think I've figured out how double quotes (")
                              >>>>>differ from single quotes ('), but if there is a discussion that
                              >>>>>someone can point me to, that would be very nice.
                              >>>>>>
                              >>>>Re: quotes - http://us.php.net/types.string
                              >>>>>
                              >>>>>
                              >>>Good try, but it has nothing to do with the user's question.
                              >>>>
                              >>His question (to quote) about "how double quotes (") differ from
                              >>single quotes (')?"
                              >>It's spot on, Jerry. Did you even bother to look? That page it has
                              >>a very thorough discussion of Variable parsing and how to use both
                              >>types.
                              >>>
                              >>>
                              >>B.S.
                              >>
                              >Read again. He was asking about back tickies (`) in MySQL statements,
                              >not single quotes (') in PHP.
                              >>
                              >It had absolutely nothing to do with PHP strings.
                              >
                              Du-uh ...... try again He spoke of both. Here I'll quote it for you
                              again:
                              >
                              >I'm learning PHP and MySQL ......
                              >
                              >..... While I'm asking, I think I've figured out how double quotes (")
                              >differ from single quotes ('), but if there is a discussion that
                              >someone can point me to, that would be very nice.
                              >
                              Enough of this waste of time already. Haven't you better things to do?
                              >
                              Case closed.
                              >
                              Yep, you obviously don't understand plain English.

                              "I'm learning PHP and MySQL. In the samples I work with, the SQL table
                              names are "escaped" in accent grave (`) marks, not in single or double
                              quotes.?"

                              That's his question - not single vs. double quotes.

                              You're right. Case closed.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...