Forums

Plugin to access and print database tables and fields (16 posts)

  1. lawrencefinn
    Member
    Posted 1 year ago #

    Hi Everyone

    I'm writing my first plugin (obveously badly so please don't rub it in, did mention this is my first attempt).

    This is the code I have to access my wp database custom tables. It's not working and I need some advice about what I am doing incorrectly and incoherently.

    snip------------

    // This gets called at the plugins_loaded action
    function getinfo()

    {

    // this attempts to call database and populate using the wordpress API
    $wpdb->query('FROM sponsors
    ORDER BY sponsors.sponsor_id ASC, sponsors.companyname ASC');
    }
    ?>

  2. GamerZ
    Member
    Posted 1 year ago #

    you need to add a global $wpdb; after function getinfo() {

  3. lawrencefinn
    Member
    Posted 1 year ago #

    Thanks, I tried it like this:

    function getnfo()
    global $wpdb;
    {

    // this attempts to call database and populate using the wordpress API
    $wpdb->query('FROM sponsors
    ORDER BY sponsors.sponsor_id ASC, sponsors.companyname ASC');
    }

    but it gave me a fatal error when trying to activate. Soz, I'm so new to this it's like being blind...

    I also tried this in a similer plugin:

    function getnfo2() {
    global $wpdb;
    $sponsors = $wpdb->get_row("FROM sponsors
    ORDER BY sponsors.sponsor_id ASC, sponsors.companyname ASC", ARRAY_A);
    echo $sponsors;
    }

    This one jsut fails to show up on the page when called by <?php getnfo2(); ?> so this might mean some progress on my part... ???

  4. RogerTheriault
    Member
    Posted 1 year ago #

    Kudos for making the attempt.

    You might want to grab a good Beginning PHP book and a MySQL (or general SQL) book... and read all the Codex docs on developing a plugin. Also, take a look at the code in other plugins and in WordPress itself (the wp-includes directory).

    Your fatal error was because the global statement belongs inside the function, which starts at the {

    Your query is probably generating an error (and not returning anything) because it is incomplete. See the wpdb Class Function Reference for details, but generally, you'll need something like

    SELECT * from $wpdb->sponsors [... etc ...]

    Then, once you get your query working, you're going to discover that

    echo $sponsors;

    won't do the trick, so check the examples on the function reference page for how to access your associative array results.

  5. lawrencefinn
    Member
    Posted 1 year ago #

    Thanks for that,

    Do you mean:

    function getnfo2()
    global $wpdb; {

    and finally

    print $sponsors;

    rather than echo $sponsors; (or is there more than this)

    I'll take a look at the clas function ref and some more beginnng php sql stuff. My big problem is that I don't have any starting reference ie I can't contextualise what I am seeing and reading, so the mroe examoples the eaiser comprehension is... :-(

  6. RogerTheriault
    Member
    Posted 1 year ago #

    You really should learn PHP basics first, unless you enjoy debugging.

    Try this: http://www.w3schools.com/PHP/DEfaULT.asP

    And for functions:

    http://www.w3schools.com/PHP/php_functions.asp

    A good beginners book will explain things as you go, in a logical sequence, and save you a lot of headaches.

  7. lawrencefinn
    Member
    Posted 1 year ago #

    thanks

  8. lawrencefinn
    Member
    Posted 1 year ago #

    Hi

    I went and read all of the articles you suggested and rewrote the plugin based on my understanding of them (ahem, bit flawed but...)

    This is what I came up with and it did load correctly

    function getnfo2() {
    global $wpdb;
    $sponsors = $wpdb->get_row("SELECT sponsors FROM $wpdb->sponsors, ARRAY_A");
    echo $sponsors;
    }

    I then called it from a template page using <?php getno2(); ?>

    and nothing displayed (which is what you said earlier would happen). I can't find a reference to it not displaying in this way (totally invisible nothingness). So if you can give me a hint as to what I ought to be looking at I really would appreciate it.

    Thanks again...

  9. RogerTheriault
    Member
    Posted 1 year ago #

    You're going to have to help yourself a bit... there's plenty of documentation, this is not a good forum to be taught PHP programming piece by piece.

    Re-read what I wrote above about associative arrays. And read the documentation for the get_row function.

    And get a good PHP book and read the chapters on debugging. A lot of the time errors will not generate anything on the browser, but there are ways to find out what happened and what you did wrong on your own.

  10. lawrencefinn
    Member
    Posted 1 year ago #

    Hmm, Would have thought that having read the information you suggested that at this point it might make sense to be shown an example of how it would or should look.

    As a professional teacher I know that telling someone to read the fu*k*ng manual is a cop out as most people do read the manual but can't understand it until they have a context.

    I'm asking what I consider to be reasonable questions (it may be that my lack of knowledge shows these up as not being reasonable and I am ignorant of this, but yur replys at this point don't really show you of in the best light).

    If your concerned that I'm here stealing code (which is the unhelpful impression I am getting then please ignore my posts in future).

    So if anyone else could assist by showing me by example what is I expect a fairly commonly utilised process in Wordpress please do. I'm not asking for someone to write the code jsut elaborate on the process.

    If your just going to tell me to go read the manual again then please get back on your high horse and ride out of town. I am sure that there are heaps of other people whose pockets are still dry who could use your help.

  11. mosey
    Member
    Posted 1 year ago #

    Not knowing much about PHP and not wanting to get em-brawled in whether 'help should be more helpful' or 'the helped should be more grateful':

    $mylink = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A);

    is just one of the examples from the Function Reference page. This (and others) always seems to have WHERE which I didn't see in the example lawrencefinn posted a little earlier. Is this a crucial part of calling from the database? Having looked at a few other Wordpress plugins in my folder, they all seem to need 'WHERE'.

  12. whooami
    Member
    Posted 1 year ago #

    As a professional teacher I know that telling someone to read the fu*k*ng manual is a cop out as most people do read the manual but can't understand it until they have a context.

    here's some quick context. We are not professional teachers, we are volunteers.

    The best way for you to learn how to write a plugin is to look at how other plugins are written. There IS your other context.

  13. hallsofmontezuma
    Member
    Posted 1 year ago #

    Mosey: you don't need the WHERE clause for every MySQL query. It selects certain rows where the WHERE condition is satisfied.

    Lawrencefinn: though we're happy to help where we can, this isn't a PHP/MySQL help forum. The best way to learn is to take a class, read books, look a code that other people wrote, or some combination of the three. There are hundreds of PHP and/or MySQL forums out there for questions, but even they won't want to help you every step of the way.

    Whooami: you don't get paid either? I thought I was just getting shafted:)

  14. hallsofmontezuma
    Member
    Posted 1 year ago #

    Lawrencefinn,

    As for why you aren't seeing any output from your echoing of sponsers,

    so check the examples on the function reference page for how to access your associative array results

    dealt with that.
    You're looking at the examples, and copy/pasting the code in hopes it will work. You need to understand at least the basics of programing and of PHP/MySQL.
    I would recommend Googling for guides and tutorials on associative arrays to learn why you aren't seeing any output.

  15. lawrencefinn
    Member
    Posted 1 year ago #

    "As for why you aren't seeing any output from your echoing of sponsers, so check the examples on the function reference page for how to access your associative array results
    dealt with that."

    In that case an apology is in order, on my part. Now that I can see the context, I can see what was missing and I can see what was trying to be communicated. Thanks also for the other pointers, I think that gives me more of a starting point.

    Frustration is such a bastard... I'll post the result once i have it figured out.

    Cheers

  16. RogerTheriault
    Member
    Posted 1 year ago #

    Thanks for chiming in everyone.

    I've done some teaching myself. And a long time ago I had the fun job (yes, paid... ) of helping CS101 students figure out why the compiler blew chunks on their code (hint: mind the semicolons). Even with five lines of code there are usually a dozen ways to do it differently and get the same result, and a hundred ways to mess it up. I can't teach you that in a simple response here.

    I guess I could have also pointed out that "echo $foo;" will print "Array" instead of your resultset. You'll probably want to use a loop to print the array values. If you just want to debug, (to see if it worked), try var_dump($sponsor) instead. Oh, and you may want to SELECT * FROM if you have more than one column named sponsor. Oh, and if your SQL caused errors and returned nothing, you'll want to peek in the error logs, view the source, or add a few debugging statements. Even a misspelling or something simple can cause an error, and they don't all show up on your browser.

    I can't stress enough... $30 on a decent Learning PHP (or PHP & MySQL) book (hopefully one as witty as the Learning Perl I still have on my bookshelf) will pay vast dividends.

    In particular, a good book that walks through the language from basic to advanced (rather than a reference or cookbook that you only open when you have a problem to solve) will teach you about those other ways to do something, and which approach is better for a given situation, so you're not limited by copying code.

Topic Closed

This topic has been closed to new replies.

About this Topic