WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Congratulations wordpress you made mysql a tedious affair :( (6 posts)

  1. carbontwelve
    Member
    Posted 8 years ago #

    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?

  2. MattRead
    Member
    Posted 8 years ago #

    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..

  3. carbontwelve
    Member
    Posted 8 years ago #

    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...

  4. carbontwelve
    Member
    Posted 8 years ago #

    Thanks Matt, i also forgot to add ARRAY_A

    At least now its sorta working :)

  5. carbontwelve
    Member
    Posted 8 years ago #

    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>';
    }

    }

    ?>

  6. davidchait
    Member
    Posted 8 years ago #

    so it wasn't so tedious after all? ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags