2017-07-20 48 views
0

如何在wordpress中從MySQL提取隨機值?從MySQL中提取隨機值到wordpress

我想用腳本創建一個按鈕生成,點擊將不得不退出從mysql提取的值,任何值只能提取3次?

數據庫名稱codefriend進入數據庫有多個值例子= 123,222,333,1231,3212

+1

您的帖子完全不可理解。請編輯它。 – piotr

回答

1

既然你沒有指定任何表,這裏是一個想法:

$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代碼,我沒有測試它。