dounieliang4712 2017-07-20 23:33
浏览 17
已采纳

从MySQL提取随机值到wordpress

How do I extract random values ​​from MySQL on wordpress?

I want to create a button with script generates, clicking will have to exit the extracted value from mysql, any value can be extracted only 3 times?

Database name codefriend and into database there are more value example= 123, 222, 333, 1231, 3212

  • 写回答

1条回答 默认 最新

  • dongshandun4363 2017-07-21 00:44
    关注

    Since you did not specify any table, here is an idea:

    $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;
        }
    } 
    

    This is just sudo code, I did not test it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题