akrisanov
akrisanov

Reputation: 3214

Simple Wordpress widget: resolving MySQL error

I'm working on a simple widget for the Wordpress CMS. The widget just supposed to render some database records. I put the widget file (function.php) into the theme folder (themes/vigilance/):

if (function_exists('register_sidebar_widget'))
{
    register_sidebar_widget('TwiMoldova','twimoldova');
}

function twimoldova()
{
    include('widgets/twimoldova.php');
}

themes/vigilance/widgets/twimoldova.php:

<?php
    echo "<p>";
?>
<div class="widget" style="border-width:thin; border-style:solid; padding: 7px; font-size: 14px;">
    <?php
        define('DB_HOST', 'host');
        define('DB_NAME', 'name');
        define('DB_USER', 'user');
        define('DB_PASS', 'pass');
        
        echo '<p align="center"><a href="http://rating.twimoldova.com/"><b>Рейтинг Twitter в Молдове</b></a></p>';
        
        echo "<b>Статистика</b>:<br/>";
        $conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);                              
        mysql_select_db(DB_NAME, $conn);
        
        $sql = "SELECT COUNT(username) FROM twitter_users;";
        $rs_result = mysql_query($sql, $conn);
        $row = mysql_fetch_row($rs_result);
        echo "Всего в рейтинге: ".$row[0]."<br/>";
        //...
    ?>
</div>

The widget can't display the data because of MySQL error: alt text

But If I run the script without Wordpress it renders table records:

alt text

Where is a catch?

Upvotes: 0

Views: 417

Answers (1)

SorinV
SorinV

Reputation: 624

The way this works in wordpress is by using the wordpress included database engine. You basically do something like this:

global $wpdb;
$Rows = $wpdb->query("SELECT COUNT(username) FROM twitter_users");

Upvotes: 1

Related Questions