Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use mysql_real_escape_string function in PHP
    primarykey
    data
    text
    <p>So in this program I'm writing, I actually grab a SQL query from the user using a form. I then go on to run that query on my database.</p> <p>I know not to "trust" user input, so I want to do sanitization on the input. I'm trying to use <code>mysql_real_escape_string</code> but have been unsuccessful in getting it to work.</p> <p>Here's what I'm trying, given the input: <code>select * from Actor;</code></p> <pre><code>//"query" is the input string: $clean_string = mysql_real_escape_string($query, $db_connection); $rs = mysql_query($clean_string, $db_connection); if (!$rs) { echo "Invalid input!"; } </code></pre> <p>This is ALWAYS giving me the</p> <blockquote> <p>"Invalid input!"</p> </blockquote> <p>error. </p> <p>When I take out the <code>clean_string</code> part and just run <code>mysql_query</code> on query, the </p> <blockquote> <p>"invalid input"</p> </blockquote> <p>message is not output. Rather, when I do this: </p> <pre><code>$rs = mysql_query($query, $db_connection); if (!$rs) { echo "Invalid input!"; } </code></pre> <p>It does NOT output</p> <blockquote> <p>"invalid input".</p> </blockquote> <p>However, I need to use the <code>mysql_real_escape_string</code> function. What am I doing wrong?</p> <p><strong>Update:</strong></p> <p>Given <code>select * from Actor;</code> as an input, I've found the following.</p> <p>Using echo statements I've found that before sanitizing, the string holds the value: <code>select * from Actor;</code> which is correct. However, after sanitizing it holds the incorrect value of <code>select *\r\nfrom Actor;</code>, hence the error message. Why is <code>mysql_real_escape_string</code> doing this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload