【问题标题】:Insert random id into database将随机ID插入数据库
【发布时间】:2013-05-29 06:33:52
【问题描述】:

我正在尝试在我的数据库中插入记录并且我正在使用此代码:

<?php 
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("my_db");

$patient_array = array();

$query = "SELECT * FROM accounts";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
$patient_array[$row['account_id']] = array("lastname" => $row['lastname'], 
"firstname" => $row['firstname']);
}

foreach($time_table as $tid => $t){
echo array_rand($patient_array, 1);
echo $tid."====".$t["from"]." - " . $t["to"]."<br />";

$sql = "INSERT INTO appointment (appt_id, appt_date, appt_time, appt_doctor, 
patient_id, appt_time_end) VALUES ('', '".$appt_date."', '".$t["from"]."', 
'".$doc."', '".$tid."', '".$t["to"]."')";

mysql_query($sql);
}
?> 

这个是正确插入的,但对于“患者 ID”,我想存储随机 ID。 $tid 给了我随机输出,但是当我检查数据库时,它不一样;它按降序显示ID。我尝试使用 Order by Rand() 但我不知道如何使它工作。任何想法?还是我错过了什么?

谢谢!

编辑:我从另一个表中获取了 patient_id,该表是要传递到约会表的帐户表。我能够选择随机的患者 ID,但如何将其插入到另一个表中,也以随机顺序。

【问题讨论】:

  • PatientID 的值从何而来?随机生成的ID 有没有范围?
  • 看看这个:titov.net/2005/09/21/…
  • 按 rand() 排序是不好的解决方案。检查此electrictoolbox.com/msyql-alternative-order-by-rand $time_table 分配在哪里我找不到它?
  • 您真的需要 PatientID 是随机的吗?可能您已经为患者提供了一些主键表,这在 99.999999% 的情况下是您想要的。那些处于 0.00001% 的人已经知道如何处理他们的情况。

标签: php mysql database random


【解决方案1】:

您可以添加一个名为“时间”的额外列,例如,您可以在其中放置 UNIX 时间,这样当您按“时间”排序时,您会按照您在表中写入它们的顺序获得随机 ID。 我建议您添加时间,将其存储在某个地方真的很有用!

如果您不希望它们按随机顺序而是按精确顺序,我实际上不明白您为什么要使用随机 id!

【讨论】:

    【解决方案2】:

    为了获取随机 id 为什么不能在 PHP 中使用函数 uniqid()

    $randomID = uniqid()
    

    请参考以下链接,

    Random/Unique ID

    【讨论】:

      【解决方案3】:

      我会创建一个名为 id 的数据库字段,其中包含 AUTO_INCREMENT 属性

      您可以使用以下命令添加列

      alter table appointment add (id INT NOT NULL AUTO_INCREMENT);
      

      【讨论】:

        【解决方案4】:

        感谢您的所有回复,但我已经找到了答案。这是我的代码修改:

        while($row = mysql_fetch_array($result))
        {
        //I changed this...
        //$patient_array[$row['patient_id']] = array("lastname" => $row['lastname'], "firstname" => $row['firstname']);
        //to this:
        $patient_array[] = $row['patient_id'];
        }
        
        foreach($time_table as $tid => $t){
        /*
        removed this, since it's just an output to check what's happening inside 
        the 'rand'      
        echo array_rand($patient_array, 1);
        echo $tid."====".$t["from"]." - " . $t["to"]."<br />";
        */
        
        //added these...
        $rand = array_rand($patient_array); //randomize patient id
        $patient_id = $patient_array[$rand];
        print_r($patient_id); //displays randomized ids
        
        //and now was able to INSERT RANDOM patient ids
        $sql = "INSERT INTO appointment (appt_id, appt_date, appt_time, appt_doctor, patient_id, appt_time_end) VALUES ('', '".$appt_date."', '".$t["from"]."', '".$doc."', '".$patient_id."', '".$t["to"]."')";
        
        mysql_query($sql);
        }
        

        【讨论】:

          猜你喜欢
          • 2011-01-30
          • 2014-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-03
          • 1970-01-01
          相关资源
          最近更新 更多