Escape Apostrophe While Checking In Db

I have an apostrophe in username and while adding that in DB first I am checking whether that username exist in DB or not but its giving error. Below is the code snippet.

$username = $_POST[‘username’];

$usr_exist = YumUser::model()->findByAttributes(array(‘username’ => $username));

How to bypass apostrophe while checking in DB. The username is like: TES’T

Have you tried addslashes()?

Yes I tried with addslashes(). Didn’t work.

I don’t think there’s a problem in you code. What specific error did you encountered?

NetworkError: 500 CDbException

Firstly, you don’t need addslashes. Yii will parameterize the query, making the input safe for you. Using addslashes will cause the value not to match.

Secondly, network error suggests that there’s an issue with your database config or your network. What’s the full error message?

Its 500 PHP Error. There should not be any issue with network configuration because only an apostrophe is causing the issue.

When I am executing the same script with str_replace its working fine. For Example,

$username = $_POST[‘username’];

$username = str_replace("’","",$username);

$usr_exist = YumUser::model()->findByAttributes(array(‘username’ => $username));

It works with no error.


$usr_exist = YumUser::model()->findByAttributes(array('username' => PDO::quote($username)));