views:

13

answers:

1

I'm trying to create a widget for Wordpress. The widget displays the records from the database.

function.php in 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/"&gt;&lt;b&gt;Рейтинг 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>

But the widget not displays the data. MySQL Error: alt text

If you run the script yourself, then the data is displayed:

alt text

Why is there a bug in the widget?

+1  A: 

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");
SorinV
Data is stored in another database, not in the database Wordpress.
Anry
In the end, need define a new database object before executing the query:$wpdb = new wpdb(MY_DB_USER, MY_DB_PASSWORD, MY_DB_NAME, MY_DB_HOST);
Anry