mysql - 拉取 vBulletin 中的所有帖子

标签 mysql vbulletin

我想创建一个脚本来计算 vBulletin 论坛中线程字数。基本上,我想从 mysql 数据库中提取该数据库并使用它。我没有使用 vBulettin 的经验,所以我正在考虑两种方法:

  1. vBulletin 是否提供 API 来处理数据库内容。 (请允许我获取所有线程内容和 URL)。我几乎可以肯定有一个从哪里开始的链接?

  2. 有没有一种解决方案可以在不受 vBulletin 干扰的情况下执行此操作。这意味着从 mysql 数据库中手动获取数据并以典型方式执行操作。

如果 vBulettin 学习曲线太陡峭,我会更喜欢第二种方法。感谢您的建议。

最佳答案

这是针对 vBulletin 3 还是 4 的?

我主要使用 vB3,包含所有 vBulletin 资源的最快方法是使用以下代码在您的论坛目录中创建一个新的 php 文件。

<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');
var_dump($vbulletin);

那个 $vbulletin 变量是注册表对象,它包含几乎所有你需要的东西,包括数据库连接和它的读写函数、用户信息数据、清理过的 _POST 和 _REQUEST 值,以及更多(短语、 session 数据、缓存等)。

您将使用最多的 4 个数据库函数。

  • $vbulletin->db->query_read()//获取多行
  • $vbulletin->db->fetch_array()//将query_read返回的数据转换成数组
  • $vbulletin->db->query_first()//获取一行
  • $vbulletin->db->query_write()//更新、插入或删除行、表等

query_read 是当您期望循环遍历多个结果时会使用的方法。例如,如果您想计算单个线程中的所有单词,则需要使用 threadid 查询帖子表,遍历该线程中的每个帖子并计算消息中的所有单词。

注意:“TABLE_PREFIX”是config.php 中设置的常量。重要的是始终在表名前加上该常量,以防其他论坛决定为其表添加前缀。

<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');

$threadid = 1;

// fetch all post from a specfic thread
$posts = $vbulletin->db->query_read("
    SELECT pagetext 
      FROM " . TABLE_PREFIX . "post 
     WHERE threadid = $threadid
");

/**
 * Loop through each post.
 *
 * Here we use the "fetch_array" method to convert the MySQL data into 
 * a useable array. 99% of the time you'll use "fetch_array" when you 
 * use "query_read".
 *
 * $post will contains the post data for each loop. In our case, we only
 * have "pagetext" avaliable to use since that's all we told MySQL we needed
 * in our query. You can do SELECT * if you want ALL the table data.
 */
while ($post = $vbulletin->db->fetch_array($posts)) {
    $totalWords = $totalWords + str_word_count($post['pagetext']);
}

/**
 * Print the total number of words this thread contains.
 *
 * The "vb_number_format" is basically wrapper of php's "number_format", but
 * with some vBulletin extras. You can visit the /includes/functions.php file
 * for all the functions available to you. Many of them are just convenient 
 * functions so you don't have to repeat a lot of code. Like vBDate(), or 
 * is_valid_email().
 */
echo sprintf("Thread ID %i contains %s words", $threadid, vb_number_format($totalWords));

query_first 函数是您需要从数据库中获取一行时使用的函数。不需要循环或类似的东西。例如,如果您想从数据库中获取单个用户的信息 - 您不需要查询,但我们将以它为例 - 您可以使用类似这样的东西。

<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');

$userid = 1;
$user = $vbulletin->db->query_first("
    SELECT *
      FROM " . TABLE_PREFIX . "user
     WHERE userid = $userid
");

echo sprintf("Hello, %s. Your email address is %s and you have %s posts",
    $user['username'], 
    $user['email'], 
    vb_number_format($user['posts'])
);

最后,如果您想更新数据库中的内容,您可以使用“query_write”。这个功能非常简单。这个函数只接受任何 MySQL 更新插入或删除查询。例如,如果我需要更新用户的雅虎 ID,你会这样做。

<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');

$userid = 1;
$update = $vbulletin->db->query_write("
    UPDATE " . TABLE_PREFIX . "user
       SET yahoo = 'myYahooID@yahoo.com'
     WHERE userid = $userid
");

if ($update) {
    $userinfo = fetch_userinfo($userid);
    echo sprintf("Updated %s yahoo ID to %s", $userinfo['username'], $userinfo['yahoo']);
}

希望这能帮助您入门。我建议您使用 vBulletin 3 一段时间,直到您对它感到满意为止。我想这对你来说会更容易。其中大部分将通过一些调整转化为 vBulletin 4,但该代码库对新手来说并不友好。

关于mysql - 拉取 vBulletin 中的所有帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7472826/

相关文章:

mysql - 将一些元组插入到 web2py 中的表中

php - JQuery 表格分页(MySQL + PHP)

php - MySQL WHERE 将连接字符串与 PHP 中的字符串进行匹配

php - 从外部站点运行网站注册过程

mysql - MySQL Workbench 中 & 符号转换为 And 字符串

mysql - 插入随机字符到MYSQL数据库

c# - 如何用C#登录vbulletin论坛?

php - 使用 PHP 自动删除 vBulletin 用户帐户

vBulletin 5.x.x 500 内部服务器错误