Home > Blockchain >  enclose variable results on single quotes
enclose variable results on single quotes

Time:12-09

Hello i have the following php code:

$singlequote = "SELECT `Email` FROM `exped_contacts_req`"; // define your SQL
$stmt = ExecuteQuery($singlequote); // execute the query
$emailtodoc = ""; // initial value
if ($stmt->rowCount() > 0) { // check condition: if record count is greater than 0
    while ($row = $stmt->fetch()) { // begin of loop
        $emailtodoc .= $row["Email"] . ", "; // in case the result returns more than one row, separated by comma
    } // end of loop
    $emailtodoc = rtrim($emailtodoc, ", "); // don't forget to remove the last comma after loop finished
} //end of check condition`

right now my variable results return as:

[email protected], [email protected]

but i need this return as:

'[email protected]', '[email protected]'.

so each email enclosed on a single quote, the problem is i can't find how reach this on my code,

any idea, thanks in advance

CodePudding user response:

Way 1:

$singlequote = "SELECT `Email` FROM `exped_contacts_req`"; // define your SQL
$stmt = ExecuteQuery($singlequote); // execute the query
$emailtodoc = []; // initial value
if ($stmt->rowCount() > 0) { // check condition: if record count is greater than 0
while ($row = $stmt->fetch()) { // begin of loop
    $emailtodoc[] = $row["Email"]; // in case the result returns more than one row, separated by comma
} // end of loop
$emailtodoc_str = implode("','",$emailtodoc); // don't forget to remove the last comma after loop finished
} //end of check condition`

Way2:

$singlequote = "SELECT `Email` FROM `exped_contacts_req`"; // define your SQL
$stmt = ExecuteQuery($singlequote); // execute the query
$emailtodoc = ""; // initial value
if ($stmt->rowCount() > 0) { // check condition: if record count is greater than 0
while ($row = $stmt->fetch()) { // begin of loop
    $emailtodoc .= "'".$row["Email"] . "', "; // in case the result returns more than one row, separated by comma
} // end of loop
$emailtodoc = rtrim($emailtodoc, ", "); // don't forget to remove the last comma after loop finished
} //end of check condition`

CodePudding user response:

Your single double quotes and single quotations need to be replaced with double double quotes and double single quotations. so " need to become "" and ' needs to become '' around what you are wrapping them around. Alternatively you should escape these characters.

Example "Escaped text looks like this \"escaped text\"."

  • Related