小编Pet*_*ter的帖子

如何从具有数千个表的mysql数据库中选择4个随机表?

我需要一种快速,有效的方法,在一个mysql数据库中随机返回4个表的名称.表没有结构化的命名模式,除了它们都是小写字母和数字.

示例:my_database包含以下表格:

(cat, dog, boat, car, cars, cars35, accorns, accerns, speaker, shell, olympics, politics, fray, ttypo, ... (thousands of tables like this) ... , road)
Run Code Online (Sandbox Code Playgroud)

我需要一种有效的方法来返回一组没有重叠的随机表的名称.

例:

function_random_table(4) would return 4 table names stored to the following variables:

$random_1 = cars
$random_2 = cars35
$random_3 = shell
$random_4 = cat
Run Code Online (Sandbox Code Playgroud)

php mysql

2
推荐指数
1
解决办法
136
查看次数

我想使用数字0-9表示mysql表名.我应该怎么用``反引号来编写sql查询来防止sql查询错误?

用户可以在我的网站上发布任何主题标题,包括纯数字(例如12345).主题标题成为mysql中表的名称.在测试一个数字作为主题标题时,我在各个地方遇到了MYSQL错误,并确定问题是我没有使用反复标记来进行sql查询.

我应该如何正确编写所有查询以保持任何可能的标题(表)不返回mysql错误?代码示例如下.还有哪些潜在的表格可能会导致问题?

<?php
for ($i=0; $i<4; $i++) {
// select by rand
$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `$random_slash[$i]` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `$random_slash[$i]` LIMIT $offset, 1 " );

//$rs = @mysql_query($sql);
while ($row = mysql_fetch_array($result)) {?>
        <div class="quadrant">
            <h2 class="alignleft"><a href="/<?php echo $random_slash[$i]; ?>">
Run Code Online (Sandbox Code Playgroud)

作为注释 - 刻度线似乎表示代码

php mysql

0
推荐指数
1
解决办法
202
查看次数

标签 统计

mysql ×2

php ×2