Replace forward to back slash in query string

867 Views Asked by At

MySql Code

SELECT replace(replace(v.image_path,"\\","/"),"%s","_120") as image_path
FROM `artistkr_fox`.`phpfox_video` v;

My phpfox service file query string

$aVideos = $this->database()->select("REPLACE(REPLACE(v.image_path,'\','/'),'%s','_120') as image_path" . Phpfox::getUserField()) ->from($this->_sTable, 'v') ->execute('getSlaveRows');

This query can't return any value

error throw in this code "REPLACE(v.image_path,'\','/')"

1

There are 1 best solutions below

0
On BEST ANSWER

You need to make sure you are not mixing the quotes of the PHP string and your SQL query.

You can do this by either by escaping them using backslashes in front of your quote (\')

$query = 'SELECT replace(replace(v.image_path,"\\","/"),\'%s\',\'_120\') as image_path FROM artistkr_fox.phpfox_video v';

or by using double quotes (") for the PHP string and single quotes (') for your SQL query:

$query = "SELECT replace(replace(v.image_path,'\\\\','/'),'%s','_120') as image_path FROM artistkr_fox.phpfox_video v";

(Note when using double quotes for the PHP string, you need to escape the backslashes in y our SQL query)

Which one you choose depends on your preference and the situation (e.g. complexity of the query).