Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved MySQL cant store strings?

Comments in 'Plugin Development' started by Andrey Nazarchuk, Aug 8, 2015.

  1. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Hello,
    How can I store, get, and set strings in MySQL?
    I tried this in my create table if not exists thing:
    Code:
    tag VARCHAR(16) DEFAULT "Guest"
    
    In the database it saves the tag column as a varchar with the value "0".
    It stays 0 even if I run:
    PHP:
    $getName "Andrey";
      
    $this->db->query("UPDATE players SET tag=Owner WHERE name = '".strtolower($this->db->escape_string($getName))."'");
    Any help?
    Thanks :)
  2. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    Try this:
    PHP:
        $getName "Andrey";
        
    $this->db->query("UPDATE players SET tag = 'Owner' WHERE name = '".strtolower($this->db->escape_string($getName))."'");
    PEMapModder and Andrey Nazarchuk like this.
  3. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Or that
    Code:
    $getName = "Andrey";
      $this->db->query("UPDATE players SET tag=Owner WHERE name = 'strtolower($getName)'");
    Andrey Nazarchuk likes this.
  4. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That definitely doesn't work and would only make one more mistake.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I recommend
    PHP:
    $name " Andrey";
    $this->db->query("UPDATE players SET tag='Owner' WHERE name='{$this->db->escape_string(strtolower($name))}'");
    BTW, MySQL by default is case-insensitive for = checks.
    Andrey Nazarchuk and Legoboy0215 like this.
  7. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    My code work in PHP things ^^ why not for plugins ?
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    tag=Owner

    On SQL you need to tag='Owner'

    And strtolower will become part of the query passed to MySQL server instead of being run by PHP.
  9. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Oops x) i dont have add '' for 'Owner' x) sorry
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And you didn't escape strtolower, so it became part of the string of the query.
  11. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Why ecape string ? "Andrey" -> strtolower -> "andrey".

    So just "andrey" work in mysql x) i use this so much time when i code in PHP and it work perfectly .
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    "strtolower('abc')"
    and
    PHP:
    "'" strtolower("abc") . "'" 
    Are different. MySQL doesn't have a function called strtolower; only PHP does.
    Andrey Nazarchuk likes this.

Share This Page

Advertisement