• I’m having a hard time getting my WP to work well with a secondary database, as it’s affecting Pages with widgets on it. Reading one topic, I moved my table into my WP database. I’m hoping it solves my problem. Anyway, here is the code. I’m not sure why it’s not working:

    <?php
    $wp_tagID = get_query_var('tag_id');  
    
    $line = $wpdb->get_results("SELECT * FROM $wpdb->playerRank");
    
    if ($line['wpID'] == $wp_tagID)  {
    
    	echo '<div class="player">';
    	echo '<div><img src="/wp-content/uploads/' . $line['nameLast'] . $line['nameFirst'] . '.jpg"></div>';
    
    	echo '<div><strong>' . $line['height'] . ' ';
    		if ($line['position'] == 'PG')  {echo 'Point Guard';}
    		elseif ($line['position'] == 'SG')  {echo 'Shooting Guard';}
    		elseif ($line['position'] == 'SF')  {echo 'Small Forward';}
    		elseif ($line['position'] == 'PF')  {echo 'Power Forward';}
    		elseif ($line['position'] == 'C')  {echo 'Center';}
    		echo '</strong></div>';
    	echo '<div><strong>' . $line['hschool'] . '<br />
    			Class of ' .$line['year'] . '</strong></div>';
    
    	echo '</div>';
    }
    
    }
    
    echo '<div class=recent>--Recent Articles--</div>';
    ?>

    Here is the best page to see what it’s doing:

    http://hoosierhoopsreport.com/tag/alexander-hutson/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter Jim R

    (@jim-r)

    There should be some profile information from my database about the kid, Alexander Hutson. That and a picture should show up between the page name and the Recent Articles header.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    $wpdb->playerRank is your problem.

    A lot of people make this mistake. They think that $wpdb->tablename is somehow referring to their table. It’s not.

    WordPress creates the $wpdb->whatever names at initialization. It does this by taking the prefix from the config file, appending it’s own table names, and then storing them as variables inside the wpdb instance of the WPDB object.

    In other words, $wpdb->playerRank is a variable. Probably one you have not set to anything.

    Set the prefix manually and see what happens. Use whatever your table prefix is, in place of the wp_

    $line = $wpdb->get_results("SELECT * FROM wp_playerRank");

    Oops, Otto is faster

    Thread Starter Jim R

    (@jim-r)

    Ok…I changed the table name, and it’s still not working.

    Thread Starter Jim R

    (@jim-r)

    Otto, while I understand a little of what you’re talking about, I’m not sure of the solution.

    Try this after your get_results statement to see if you are getting what you expect:

    echo "<pre>"; print_r($line); echo "</pre>";

    Thread Starter Jim R

    (@jim-r)

    I get this for each one of my entries.

    [0] => stdClass Object
            (
                [id] => 1
                [year] => 2010
                [position] => PG
                [grouping] => 1
                [rankClass] => 7
                [deviation] =>
                [rankPos] => 1
                [nameFirst] => Jesse
                [nameLast] => Berry
                [wpID] => 40
                [height] => 6'1"
                [level] => H-
                [hschool] => Lafayette (Jeff)
                [college] => Dayton
                [committed] => y
            )

    Then echo out the value of $wp_tagID after you get the query_var…

    Thread Starter Jim R

    (@jim-r)

    Well, two problems I can see on the surface then. First, that’s it already is set up to do, and it has worked with the exception of shutting down the other widgets (that was another topic). Second, the actual result should just be one set of that, instead of 435 sets of it, and it should be for the one matching….

    if ($line['wpID'] == $wp_tagID)
    Thread Starter Jim R

    (@jim-r)

    In other words, the link provided above, $wp_tagID = 68. So it should look for the one where wpID = 68 and print that one. It worked using a regular mysql query, but that caused my widgets to stop working. Then I read somewhere the table should be in the same database as WP for fewer conflicts, and then I saw the stuff about $wpdb.

    Thread Starter Jim R

    (@jim-r)

    Ok…been playing around with it. To review, I put my table in the same database at WP. It was suggested it would have fewer conflicts due to interacting with multiple databases. What I have done is used a regular MySQL query, and it worked.

    I have my data, and my widgets work. I’m connecting to my WP database.

    Thread Starter Jim R

    (@jim-r)

    I do appreciate all the time you guys put into the responses. Even if the direct advice isn’t the result, which most of the time it is, it gets me thinking of other things to try as I try to work through the logic.

    So don’t you need a FOREACH to loop through that array to find the appropriate item?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘New to $WPDB, can’t figure out syntax…’ is closed to new replies.