Issue with wordpress calling query to database

Hey guys so I am trying to access my database which is within wordpress to get some field values and I tried doing just straight up PDO style but it seems to not work so I went into wordpress codex and did their way to call things, but still no success!

CODE:

/***GET USERNAME***/
            global $current_user;
            get_currentuserinfo();
            $accusername = $current_user->user_login ;
            /******SEE IF FIRST TIME DISCOUNT CODE BEEN USED*******/
            $wpdb->query( 
                        $wpdb->prepare( 
                "
                        SELECT firsttime_discount
                        FROM $wpdb->users
                        WHERE user-login = %d

                ",
                    $accusername 
                                       )
                        );
                /******CHECK IT******/
                echo"working";
            $wpdb->query('query');
            echo"working";
            if ($checkFTDiscount->firsttimediscount != 1){
                    $validFTDiscount = 1;
            }
            else{
                $validFTDiscount = 0;
                echo"wori"; 
            }

So it's suppose to go in and see if the value for discount code is set to 1 in the wp_user area, and if not just set a value to 0.

Let me know if you have any other questions.

Solutions

Before you can use $wpdb, you must declare global $wpdb;

    global $current_user, $wpdb;

Should do the trick.

Tags: PHP / Mysql / Wordpress

Similar questions

Calling wp-load.php to load 3 recent posts from Wordpress Blog on Main site, now can't switch back to other database
I have a main site at www.mydomain.com. I have a wordpress blog set up at www.mydomain.com/blog. I want visitors to my main site to see the 3 recent blog posts. I was able to pull it off easily using the following code: Problem now is, anything on the page that requires calling a table for the main site no longer works. I'm getting an error saying ...
Cleanest/Fastest way to avoid calling and retrieving data from the database multiple times?
So this may be a very noob question, but I believe if I can get the answer to this I can clean up code and speed things up significantly. I build projects using Woocommerce quite a lot, and in doing so I find myself writing functions that often use the same calls from other functions. For example: or For example. Those are used quite often througho...
Database connection issue, couldn't get the data from database
I'm creating a custom registration page in wordpress. So I create a testing page to test the database connection. But the isn't displaying any out. Why is that so? Please help. (All the server into is correct)
which CSS query method is the best to use:container query.element query or media query?
I like to know which kind of queries I have to use because this is my first website I am developing and I am working with foundation grid system. I see at this moment you have containers queries which is more targeted to an element . also I see you have element queries and media queries. to have modern responsive website which one I have to use and...
Wordpress: So I've been calling wp_(tablenames) directly in mysql query. What's the best way to correct this?
So I've been developing a plugin in wordpress, which has been running very slowly, and have been making database calls directly to wp_usermeta, which I've just found out you should never call directly, as follows: $getUserdaetails = pdo_query("Select wp_usermeta.user_id, Min(Case meta_key When 'name_of_your_facility' Then meta_value End...
Calling specific page with wp query
I'm making one page website and I added a custom field on "Page" post type that asks "Use this page as a section:" Yes or No(with dropdown). I called all pages on template-onepage.php(custom template) using wp query Now what I want is to call only those page that set "Use this page as a section" to yes.. How can I achieve it ??? Please help.. Thank...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.