Martin Myška
Martin Myška

Reputation: 61

How to replace content of mysql table by cron?

i have now problem with resolving this problem. I want to run with cron (every midnight) which will replace specified content in my mysql table (im talking about wordpress database) with just one space. I want after that run with cron script which will add to every post specified text. But, if i try it with PHP, there is problem with inserted code which is really long and mysql says me that string is wrong.

That code is:

<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//cz.search.etargetnet.com/generic/uni.php?g=ref:47349,background_color:ffffff,background_image:clasic,background_opacity:95,border_color:ffffff,border_style:none,design_name:yellow,font:verdana,fsi:10,h_text_color:000000,h_title_color:948000,h_title_underline:0,h_url_color:948000,h_url_underline:0,hover_back:transparent,logo:1,logo_type:5,area:468x60,show_in_fb:1,show_in_fb_text:na google play CZ Vtipy,tabl:4,text_color:000000,title_color:948000,title_underline:0,url_color:948000,url_underline:0"></script>

Can you please help me?

EDIT: This is PHP code im trying to run but with errors

<?php

if (!$link = mysql_connect('localhost', 'user', 'pass')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('databasename', $link)) {
    echo 'Could not select database';
    exit;
}

$sql    = 'UPDATE wp_posts SET post_content = REPLACE (post_content,'<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//cz.search.etargetnet.com/generic/uni.php?g=ref:47349,background_color:ffffff,background_image:clasic,background_opacity:95,border_color:ffffff,border_style:none,design_name:yellow,font:verdana,fsi:10,h_text_color:000000,h_title_color:948000,h_title_underline:0,h_url_color:948000,h_url_underline:0,hover_back:transparent,logo:1,logo_type:5,area:468x60,show_in_fb:1,show_in_fb_text:na google play CZ Vtipy,tabl:4,text_color:000000,title_color:948000,title_underline:0,url_color:948000,url_underline:0"></script>',' ')';
$result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row['foo'];
}

mysql_free_result($result);

?>

EDIT2:

When im trying to launch PHP script, it shows:

PHP Parse error:  syntax error, unexpected 'type' (T_STRING) in /home/xxxxx/xx/xxx/xxxx/xxxx_mysql.php on line 13

Upvotes: 0

Views: 151

Answers (1)

milan kyada
milan kyada

Reputation: 579

Problem is with quotes.
You should remove single quotes from query.

And you can try this also

$link = mysqli_connect('localhost', 'user', 'pass','db') or die("Connection error");

$data = <<<EOT
<script type="text/javascript" async="true" data-ad-type="iframe v2.0" charset="utf-8" src="//cz.search.etargetnet.com/generic/uni.php?g=ref:47349,background_color:ffffff,background_image:clasic,background_opacity:95,border_color:ffffff,border_style:none,design_name:yellow,font:verdana,fsi:10,h_text_color:000000,h_title_color:948000,h_title_underline:0,h_url_color:948000,h_url_underline:0,hover_back:transparent,logo:1,logo_type:5,area:468x60,show_in_fb:1,show_in_fb_text:na google play CZ Vtipy,tabl:4,text_color:000000,title_color:948000,title_underline:0,url_color:948000,url_underline:0"></script>
EOT;

$sql    = "UPDATE wp_posts SET post_content = REPLACE (post_content,".$data.",' ')";
$result = mysqli_query($link, $sql);

Upvotes: 1

Related Questions