i'm trying to explode my variable but it has a single quote which is invalid in my mysql syntax. can u help me how to explode a single quote??

list($a, $b) = explode("'", $des);

Dani AI

Generated

Quick clarification: splitting on an apostrophe is a string operation, not a MySQL bug. The code shown in the thread uses explode with an apostrophe delimiter — that will split the string fine. The SQL error happens when an unescaped single quote from the value is injected into a hand-built query. is correct that list() isn't required to get the parts; is also correct that escaping matters, but escaping a PHP literal (backslash inside source) is different from escaping a value for SQL.

Safer ways to split by an ASCII apostrophe or to catch common apostrophe-like characters:

$parts = explode(chr(39), $des, 2);

or to handle both ASCII apostrophe and the Unicode right single quote:

$parts = preg_split('/[\x27\x{2019}]/u', $des);

Use the limit argument or check count($parts) before using list() so notices and undefined values are avoided.

Important: do not rely on manual backslash-escaping for SQL. The recommended approach is parameter binding / prepared statements. Example with PDO:

$stmt = $pdo->prepare('INSERT INTO mytable (col) VALUES (:val)');
$stmt->execute([':val' => $des]);

If using mysqli and a prepared statement is not possible, use mysqli_real_escape_string() to escape the value before concatenating into a query (not addslashes() as a long-term substitute).

Extra tips: var_dump() the original string to check for curly/HTML-encoded quotes (e.g., ’). If list() will be used, guard it with a count() check or use null-coalescing so missing parts default to an empty string. Logging the final SQL (temporarily, not in production) often shows exactly where the unescaped quote breaks the syntax.

Recommended Answers

All 2 Replies

why you are using list()?
to retriev result fro the explode("'",$des);
just store in variable...
It will work....

Like the guy above says you dont need to use list...You need to escape the ' though with a \

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.