Convert MYSQL to WordPress $WPDB

Failing to convert my PHP after I upgraded my WordPress site to 4.3.1.

The original code was:

Read More
$result = mysql_query("SELECT *,
                        CASE Position 
                          WHEN '1' THEN 'First'
                          WHEN '2' THEN 'Second'
                          WHEN '3' THEN 'Third'
                        END as PositionLong
                      FROM People 
                      WHERE
                         (Position='1' or
                          Position='2' or
                          Position='3') and
                          Season = '2015'
                      ORDER BY Number");
while($row = mysql_fetch_assoc($result)){
  echo '<div class="column-left"><img src="/wp-content/uploads/profile/'.$row['Id'].'.jpg" alt="" />',
       '<h3 class="widget-title"><a href="profiles?title='.$row['Id'].'">',
        $row['FirstName'].' '.$row['Surname'].'</a></h3><br />',
       '<pos>'.$row['PositionLong'].' #'.$row['Number'].'</pos></div>';       
                                       }

So… I tried reading some of the guides etc and tried… replacing to:

$result = $wpdb->get_results("SELECT *,
                        CASE Position 
                          WHEN '1' THEN 'First'
                          WHEN '2' THEN 'Second'
                          WHEN '3' THEN 'Third'
                        END as PositionLong
                      FROM People 
                      WHERE
                         (Position='1' or
                          Position='2' or
                          Position='3') and
                          Season = '2015'
                      ORDER BY Number");
foreach ($result as $row){
  echo '<div class="column-left"><img src="/wp-content/uploads/profile/'.$row['Id'].'.jpg" alt="" />',
       '<h3 class="widget-title"><a href="profiles?title='.$row['Id'].'">',
        $row['FirstName'].' '.$row['Surname'].'</a></h3><br />',
       '<pos>'.$row['PositionLong'].' #'.$row['Number'].'</pos></div>';       
                                       }

However I end up with:

Fatal error: Call to a member function get_results() on a non-object

Any pointers greatly received! I have a few pages showing sod all at present 🙂

Thanks

Mongo

Related posts

1 comment

  1. You most likely need to get the WPDB global.

    Try adding

    global $wpdb;
    

    to the top of your script?

Comments are closed.