ORDER BY RAND无法正常工作

use*_*914 6 php mysql database

希望一切顺利.我有一些问题,没有随机选择一个IP.我会尽力解释.

<select name="State">
<option value="0" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
   etc.....
</select>
Run Code Online (Sandbox Code Playgroud)

任何时候客户选择一个状态并提交它进入我的数据库的表单并将一个ip地址拉回到该状态.这就是我的数据库的样子

+-------+---------------+
| state |      ip       |
+-------+---------------+
| AL    | 67.100.244.74 |
| AK    | 68.20.131.135 |
| AZ    | 64.134.225.33 |
+-------+---------------+
Run Code Online (Sandbox Code Playgroud)

感谢在这个论坛上的人我有一些PHP代码,当提交表单时收集IP地址,并将其发送到我的电子邮件.完善.这是代码

<?php
// visit http://php.net/pdo for more details
// start error handling

try 
{
  // connect
  $pdo = new PDO('mysql:host=localhost;dbname=name', 'dbuser', 'pass');
  // enable error handling through exceptions
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  // create safe query
  $query = $pdo->prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY RAND() LIMIT 1");
  // pass data & execute query (since the data are of string type
  // and therefore can be passed in this lazy way)
  $query->execute(array($_POST['State']));
  // get value
  $ip = $query->fetchColumn();
  // print out the IP address using $ip
}
catch (Exception $e)
{
  echo "sorry, there was an error.";
  mail("email@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
}
?><?php

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "1stoptutorials@gmail.com";
    $email_subject = "This is a test";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['State']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $state = $_POST['State']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "State: ".clean_string($ip)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (!mail($email_to, $email_subject, $email_message, $headers))
{
    echo "failed to send message";
}  

?>
Run Code Online (Sandbox Code Playgroud)

它唯一没有做的就是从州获取随机ip.对于每个状态,即AL,AK,AZ等.我有大约150个不同的IP地址.所以,让我们说有人选择阿拉巴马州(AL)的状态,我希望它从与AL在同一行的数据库中随机选择和ip.它正在接收IP地址确定,但总是向我发送相同的IP用于AL.

根据代码它应该这样做,因为ORDER BY RAND就在那里

$query = $pdo->prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY RAND() LIMIT 1");
Run Code Online (Sandbox Code Playgroud)

我四处询问,有些人建议我需要在我的表中使用另一个名称为id的列,所以这应该是它的样子

-------+-------+---------------+
| id   | state |      ip       |
+------+-------+---------------+
|  1   | AL    | 67.100.244.74 |
|  2   | AK    | 68.20.131.135 |
|  3   | AZ    | 64.134.225.33 |
+------+-------+---------------+
Run Code Online (Sandbox Code Playgroud)

他们说我需要id,所以我可以随机拉入ip.有谁知道我需要添加此ID的PHP代码,以使这一切都工作.任何帮助将非常感激

感谢大家

阿里

Joh*_*ica 3

RAND()每次都会给出不同的值。
如果您不希望这样,您可以给出固定的种子值。
如果您很偏执,您可以每次提供不同的种子值。

SELECT ip FROM vincer WHERE state = ? ORDER BY RAND(UNIX_TIMESTAMP(NOW())) LIMIT 1  
Run Code Online (Sandbox Code Playgroud)

如果一个州内的不同 ip 很少,则当 ip 数量接近 1 时,看到相同 ip 的机会就会增加。
如果只有 1 那么当然会返回。

您可以RAND()通过将查询更改为以下内容来查看所使用的:

SELECT @rand:= RAND() as rand, ip 
FROM vincer 
WHERE state = ? 
ORDER BY rand 
LIMIT 1  
Run Code Online (Sandbox Code Playgroud)