php - 从 MySQL 中提取随机值到 WordPress

标签 php mysql wordpress

如何从 WordPress 上的 MySQL 中提取随机值?

我想创建一个带有脚本生成的按钮,单击将必须从mysql中退出提取的值,任何值只能提取3次?

数据库名称codefriend并进入数据库还有更多值example= 123, 222, 333, 1231, 3212

最佳答案

由于您没有指定任何表格,这里有一个想法:

$arr_values = array();
$arr_tables = get all tableNames in codefriend-DB. mysql has function to do that.
for ($i = 0; $i < count($arr_tables); $i++) {
    // for each table, get field name list
    $arr_fields = get fields name from mysql
    $total_records = get total record for this table from mysql

    $random_fields = rand(0, count(arr_fields)-1); // get random number between 0 and last index of fields array
    $random_id = rand(1, total_records);

    // get the value from sql query
    $query = "SELECT ".$arr_fields[$random_fields]." FROM ".$arr_tables[i]." WHERE id =".$random_id;
    $string = mysql_result($query);   // get the value to array

    $total_extractions = array_count_values($arr_values);// this will list all occurance for each value
    if($total_extractions[$string] < 3 )
    {
       // append the value
       $arr_values[] = $string;
    }
} 

这只是 sudo 代码,我没有测试它。

关于php - 从 MySQL 中提取随机值到 WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45226744/

相关文章:

php - cUrl 打开多个 URL

php - nginx 返回 200 但响应为空

php - 在 Web 应用程序中简化并提高表单/crud 操作的效率?

mysql - 如何在 W TO PUT NOT EQUAL IN CASE THEN 语句中使用 <>(不等于)

wordpress - IIS7 URL 重写的日语/国际字符问题

php - 如何通过jQuery Steps上传图片

php - MySQL @grouping results by ID and sorting them by score”问题

wordpress - 如何在 woocommerce 中显示产品相关新闻?

mysql - 在 WooCommerce 中添加订单时 WordPress MySQL 出现错误

php - 变量在 whereExist 函数内未定义 - laravel