如何从 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/