Results 1 to 12 of 12

Thread: [Resolved]PHP problem with '

  1. #1

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Resolved [Resolved]PHP problem with '

    Code:
    $sql = "SELECT mem_id FROM t_user where user_name='$musername' and user_password='$pwd'";
    $result = mysql_db_query($db, $sql);
    $row = mysql_fetch_array($result);
    I will got an error if any input contain '
    Why this happen, I'm sure that this code should work fine

    Is there any problem about PHP version?
    Last edited by naruponk; Aug 18th, 2006 at 12:45 PM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: PHP problem with '

    Think about it: the $ expression in the string is replaced by the content of the variable. Take the resulting SQL query and search for what's wrong with it.

    The issue is the root of the security problem called SQL injection. The direct solution is to use the addslashes() function to escape the parameter so that you can use it. However, this solution is error-prone. It is better not to use the old mysql API and instead use any of several better DB APIs that support prepared statements. Prepared statements contain parameter placeholders that you can then assign values to, and the API takes care of escaping the parameters.
    APIs that support PSs are:
    MySQLi (MySQL improved): MySQL-only, PHP5-only
    PEAR::MDB2: Cross-DB, cross-version. See http://pear.php.net/ for details. Look for MDB2.
    PDO (PHP Data Objects): Cross-DB, PHP5-only. Comes by default with PHP5.1. Preferred for PHP5-only development.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP problem with '

    Also, magic quotes should be on by default, you or your host must have disabled them somehow. Buuuuuut, I second CB's suggestion.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: PHP problem with '

    Magic quotes are evil. They disguise the problem, instead of solving it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: PHP problem with '

    Thanks for suggestion

  6. #6
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: PHP problem with '

    you are getting the error because single quotes(') is a reserved SQL character but it is possible you escape it with a backslash.

    like this

    PHP Code:
    $string "that's it";
    $string str_replace("'""\'"$string); 
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP problem with '

    Read CB's post again. If you must manually replace them, use addslashes(). But you should be using prepared statements instead so that you avoid all this nonsense.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: PHP problem with '

    Quote Originally Posted by modpluz
    you are getting the error because single quotes(') is a reserved SQL character but it is possible you escape it with a backslash.

    like this

    PHP Code:
    $string "that's it";
    $string str_replace("'""\'"$string); 
    At the very least you should use a database specific routine such as mysql_escape_string(). Of databases use different escape sequences. Access and MSql for example use '' and VFP doesn't even have them, so they must be replaced in SQL using ' + CHAR("'") + '
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: PHP problem with '

    Code:
    $sql = "SELECT mem_id FROM t_user where user_name=". '"'. $musername. '"'. ' and user_password='. '"'. $pwd. '"';
    I just now using this one to fastly resolving problem
    Any one think is there any problem?

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP problem with '

    You haven't solved the problem at all. You've just made it so double quotes will stuff it up rather than single quotes.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: PHP problem with '

    Why is it that people so desperately search for wrong AND hard ways to solve problems, when the easy and correct way is laid out for them?

    Code:
    require_once('MDB2.php');
    
    $dsn = 'mysql://user:password@localhost/database';
    $db =& MDB2::connect($dsn);
    
    $query =& $db->prepare('SELECT mem_id FROM t_user WHERE user_name = ? AND user_password =?');
    $results =& $query->execute(array($musername, $pwd));
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Hyperactive Member naruponk's Avatar
    Join Date
    Feb 2004
    Location
    Some where in the world
    Posts
    423

    Re: PHP problem with '

    Above reply are very usefully and works fine.
    but last one is wonder.

    I will try with this,

    Thanks for all helps
    Last edited by naruponk; Aug 18th, 2006 at 12:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width