0

我有一个 MySQL 数据库,我需要一个 PHP 来拉随机行。我已经成功创建

$query = "SELECT * FROM $usertable
          WHERE region='UK'          
          ORDER BY RAND() LIMIT 1";

这成功地随机拉了一行;但是,它不限于 where region=2

我需要能够:

  1. 时随机拉region=UK
  2. 随机拉时region=UK或##

(其中 ## 实际上是另一个区域,例如 YK = 约克郡)

基本上我需要它来随机选择行,但仅在region=UK.

region是 my 之一的标签,fields/collumns是多行的 in thatUK的内容。VARCHAR

我对其余的代码进行了排序。

我有一个简单的数据库和 php 如下:

<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="carbonmarketing.db.9606426.hostedresource.com";
$username="MarketReadOnly";
$password="Read0nly1";
$dbname="carbonmarketing";
$usertable="ClientList";
$advertfooter = "advertfooter";
mysql_connect($hostname,$username, $password) or die ("<html>%MINIFYHTML4333ddb1f6ba50276851b9f9854a5c817%</html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = "SELECT * FROM $usertable
          WHERE region='UK'          
          ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
if($result)
{
    while($row = mysql_fetch_array($result))
    {
        $advertfooter = $row["$advertfooter"];
        echo "$advertfooter";

    }
}
?>

但是,它只是随机抽取region列的所有值

让我知道它是否有助于您查看数据库。

4

2 回答 2

2

制作和排列您的区域并内爆它们:

$region = array('UK', 'YK');
$implode = implode("', '", $region);
$query = "SELECT * FROM `".$usertable."` WHERE `region` IN ('".$implode."') ORDER BY RAND() LIMIT 1";
于 2012-10-01T11:56:20.167 回答
1
$query = "SELECT * FROM $usertable
          WHERE region IN ('UK','YK')          
          ORDER BY RAND() LIMIT 1";
于 2012-10-01T11:56:07.877 回答