Using special characters in a MySQL Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
morris4019
Forum Newbie
Posts: 1
Joined: Wed Sep 02, 2009 1:47 pm

Using special characters in a MySQL Query

Post by morris4019 »

Hello, kind of new to MySQL, but i'm trying to learn. Anyway, i'm having trouble running a SELECT query. Let me explain my situation. I am building a website for a World of Warcraft Guild. I am creating a registration page, which will ask for all the usual information from the user, including a Game Name. Now I have populated a seperate table with all the guild members. The 1 in particular that i am having problems with is "Mäg" (abviously without the quotes). Now the filter checks that the username is not taken, and so on. But when it comes to checking whether or not the Game Name is valid, every one works except this guy. For some reason it will not return a row. Now i have checked and rechecked that the form is getting the data to the query correctly. It started that it actually encoded it different once i hit the submit button, but i have gotten it to be identical by using the mysql_real_escape_string function. Anyway, this is the code i am running.

Code: Select all

 
    $dbh = mysql_connect($cfg['host'],$cfg['user'],$cfg['pass']);
    mysql_select_db($cfg['database']);
    
    
    $query = sprintf("SELECT * FROM guild_members WHERE game_name='%s' LIMIT 1",
            mysql_real_escape_string($_POST['game_name']));
    
    print $query."<BR>"; // Print out the query to debug.
    
    $result = mysql_query($query);
 
    if (mysql_affected_rows($dbh)=='1')
    {
        print "worked<BR>";
    }
 
[/color]



Now the code all runs on one page. The forms action is to call PHP_SELF and it has a hidden field named action with a value of "register". Now the data all seems to stay correct through the transition to the second run on the page. But when this query is run, only the names without the special characters work.

Any ideas? I really need this page to work correctly.
Post Reply