Results 1 to 3 of 3

Thread: Queries execute via phpmyadmin but not via php script

  1. #1
    Join Date
    Apr 2014

    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?

    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);

  2. #2
    Join Date
    Mar 2005
    Behind you...
    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.

    PHP Code:
    $dbh = new PDO('mysql:host=localhost;dbname=DATABASE_NAME''MYSQL_USER''MYSQL_PASSWORD');

    $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);"
    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).
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Aug 2004
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles