这是针对 vBulletin 3 还是 4 的?
我主要使用 vB3,包含所有 vBulletin 资源的最快方法是使用以下代码在您的论坛目录中创建一个新的 php 文件。
<?php
error_reporting(E_ALL & ~E_NOTICE & ~8192);
require_once('./global.php');
var_dump($vbulletin);
$vbulletin 变量是注册表对象,其中包含您将需要的几乎所有内容,包括数据库连接及其读写函数、用户信息数据、已清理的 _POST 和 _REQUEST 值,以及更多(短语、会话数据,缓存等)。
您最常使用的数据库函数有 4 个。
- $vbulletin-> db ->query_read() // 获取多于一行
- $vbulletin-> db ->fetch_array() // 将 query_read 返回的数据转换成数组
- $vbulletin-> db ->query_first() // 获取单行
- $vbulletin-> db ->query_write() // 更新、插入或删除行、表等
query_read是当您希望循环遍历多个结果时使用的。例如,如果您想计算单个线程中的所有单词,则需要使用 threadid 查询 post 表,遍历该线程中的每个帖子并计算消息中的所有单词。
注意:“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 更新插入或删除查询。例如,如果我需要更新用户的 yahoo 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,但该代码库对新来者并不友好。