0

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?

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Data is stored in another database, not in the database Wordpress.
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.