Support » Plugins » Congratulations wordpress you made mysql a tedious affair :(

  • Resolved carbontwelve

    (@carbontwelve)


    I need to run this query in my plugin:
    $sql = ‘ SELECT zen_images.filename AS filename, zen_images.desc AS file_desc, zen_album.folder AS folder, zen_images.title AS title’
    . ‘ FROM zen_images AS zen_images’
    . ‘ INNER JOIN zen_albums AS zen_album ON zen_album.id = zen_images.albumid’
    . ‘ WHERE zen_images.show =1’
    . ‘ ORDER BY zen_images.id DESC’
    . ‘ LIMIT 0 , 3’;

    But for some reason wordpress breaks every attempt i have made to run the above querry (which is perfecly legal, as it runs perfectly in phpmyadmin).

    I need to run it, and put the result in an array which i can loop though echoing each returned row.

    Can someone please help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Are you running the query with the $wpdb object. like:

    $wpdb->get_results($sql, ARRAY_A);

    The ARRAY_A tells it to return as an associative array. And remember to global $wpdb if inside a function.

    Also that will only work if the zen_images table is in the same DB as WP. If not you’ll have to create your own mysql_connect etc..

    Ok people, i didnt realise that you had to global $wpdb, well not until I ran though the sample code I got from the live archive plugin again.

    Honest mistake to make I guess…

    Thanks Matt, i also forgot to add ARRAY_A

    At least now its sorta working 🙂

    My Final Code is this:

    <?php

    /*
    Plugin Name: ZenGo
    Plugin URI: http://www.photogabble.co.uk
    Description: Displays the recent additions to the zenphoto gallery
    Version: 0.5
    Author: Simon Dann
    Author URI: http://www.photogabble.co.uk
    */

    function zenlatest ($max){
    global $wpdb;
    if ($max == "") { $max = 1; }

    $zenimages = $wpdb->get_results("SELECT zen_images.filename AS filename, zen_images.desc AS file_desc, zen_album.folder AS folder, zen_images.title AS title FROM zen_images AS zen_images INNER JOIN zen_albums AS zen_album ON zen_album.id = zen_images.albumid WHERE zen_images.show =1 ORDER BY zen_images.id DESC LIMIT 0, ". $max ."");

    foreach ($zenimages as $zenimage ) {
    echo '<a href="/zenphoto/'. $zenimage->folder .'/'. $zenimage->filename . '"><img src="/zenphoto/'. $zenimage->folder .'/image/thumb/'. $zenimage->filename .'"></a>';
    }

    }

    ?>

    so it wasn’t so tedious after all? 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Congratulations wordpress you made mysql a tedious affair :(’ is closed to new replies.