Text Widget to Show database fields based on post

I can’t find a plugin that does this, but here’s an alternative:

You can use code like this to get the post’s metadata (custom field) values in the widget sidebar.php:

<?php global $post; ?>
<?php $sidebar = get_post_meta($post->ID, 'sidebarval', $single = true); ?>
<?php if($sidebar != '') { ?>
    <?php $myrows = $wpdb->get_results( "SELECT id, name FROM mytable WHERE col = " . $sidebar );
<?php } ?>