PDO: stripslashes when getting results

天大地大妈咪最大 提交于 2019-12-06 07:33:57

问题


I am using PDO prepared statements so it's adding slashes when it's needed before inserting into the database.

I was wondering the proper way to get the results and display it on the website without showing the slashes.

Is it as easy as just using echo stripslashes($result->message);?

Here is what my queries look like:

$database->query('INSERT INTO table_name (field1, field2, field3) VALUES (?, ?, ?)', array($value1, $value2, $value3));

Here is my query method:

public function query($query, $bind=null)
{
    global $pdo;

    # Prepare Statment
    $this->statement = $this->pdo->prepare($query);

    # Execute Query
    $this->statement->execute($bind);
}

EDIT: get_magic_quotes_gpc was indeed turned on even though WHM (cPanel) said it was off


回答1:


Prepared statements do not add slashes to your query data. They inject the parameters into the query in the form of placeholder, in such a way that the placeholder is not considered as part of the query, but as part of the data only.

Therefore, no slashed are added, and no need for stripslashes().

If slashes are added for you, make sure you disable prepared statements emulation for your PDO instance:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);



回答2:


get_magic_quotes_gpc was indeed turned on even though WHM (cPanel) said it was off



来源:https://stackoverflow.com/questions/12222048/pdo-stripslashes-when-getting-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!