/    Sign up×
Community /Pin to ProfileBookmark

Queries execute via phpmyadmin but not via php script

Hello, I’m new with mysql and I encountered the following problem – I’ve got 2 mysql queries below and I try to execute them via php but syntax error appears. When I execute them via phpmyadmin it works like a charm showing proper results. I think I need separator to do multiple queries in one execution, can you help me with this showing how such correct script should look like?

[CODE]DELETE a FROM wp_posts AS a INNER JOIN ( SELECT post_content, MIN( id ) AS min_id FROM wp_posts WHERE post_type = ‘post’ AND post_status = ‘publish’ GROUP BY post_content HAVING COUNT( * ) > 1 ) AS b ON b.post_content = a.post_content AND b.min_id <> a.id AND a.post_type = ‘post’ AND a.post_status = ‘publish’;

DELETE FROM wp_posts

WHERE post_content NOT LIKE ‘%<img src=%’AND ID not in (select post_id as p from wp_postmeta where meta_key like ‘_wp_attached_file’)

OR `post_title` like ‘%.png%’

OR post_date < DATE_SUB(NOW(), INTERVAL 15 day);[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyApr 01.2014 — I know it is possible to execute multiple SQL queries in PHP, however I do not know all of the specifics. I use the PDO method so I'll go with that for this post. I honestly can't say if the typical mysql or mysqli is able to what you want though.

[code=php]
$dbh = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'MYSQL_USER', 'MYSQL_PASSWORD');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$q = $dbh->prepare("DELETE a FROM wp_posts AS a INNER JOIN ( SELECT post_content, MIN( id ) AS min_id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY post_content HAVING COUNT( * ) > 1 ) AS b ON b.post_content = a.post_content AND b.min_id <> a.id AND a.post_type = 'post' AND a.post_status = 'publish';

DELETE FROM wp_posts

WHERE post_content NOT LIKE '%<img src=%'AND ID not in (select post_id as p from wp_postmeta where meta_key like '_wp_attached_file')

OR post_title like '%.png%'

OR post_date < DATE_SUB(NOW(), INTERVAL 15 day);");
$q->execute();
[/code]


I'll also note that PHP should be returning a relative location where the syntax error occurs in the query. Might be something to look in to (and post if the problem persist).
×

Success!

Help @mark069 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...