Search code examples
mysqlphpmysql-real-escape-string

mysql_real_escape_string in PHP CLI


I have a script that I want to cron scheduled. Its all fine and dandy when I tested in the browser, working as it should but when run from php cli (php cron.php), mysql_real_escape_string loses the value given.

Any idea why?

UPDATED with code and a connection made before mysql_real_escape_string (but still not working)

$dbh = new PDO("mysql:host=localhost;dbname=xxx", 'xxx', 'xxx');  
foreach ($prosArr[$i] as $val => $key) {
    $fieldsStr  .= "`".trim($val). '` , ';
    $fieldVal   .= '"'.mysql_real_escape_string($key). '" , ';
}

Here is output print_r of $prosArr[$i] obtained straight from the same CLI script

Array
(
    [ProductCode] => 10077
    [BranchCode] => 100
    [RetailPrice] => 499.0000
    [PromotionPrice] => 0.0000
    [FullPrice] => 499.0000
)

Solution

  • Do not use mysql_real_escape_string() with PDO. They are different libraries and have are not meant to be used with each other.

    Use PDO::Quote or parametrized queries instead.