如何生成随机id

时间:2014-01-13 17:13:39

标签: php mysql

我想知道如何为任意数字的照片的id生成随机数,并为用户ID生成随机ID,例如:

num =照片ID idr =用户ID

yourphoto.php?num=60&idr=3

现在我想要做的是生成一些不会告诉用户照片是什么ID号或用户ID是随机的东西。

喜欢那样:

yourphoto.php?num=654654654648&idr=34546545621

1 个答案:

答案 0 :(得分:3)

检查uniqid,它将满足您的需求。

有点伪代码(与评论相关)

   //Insert into DB
    $new_photo = array(
        array('id' => 1, 'name' => 'newphoto', 'unique' => uniqid()),
        array('id' => 2, 'name' => 'newphoto2', 'unique' => uniqid())
    );

   // select everything from db
   $photos = $new_photo;

    //display photos
    foreach($photos as $photo){
            // display Link with $photo['unique'], <a href="link.php?num=$photo['unique']"
           var_dump($photo);    
    }


//GET Num
// select where unique = $_GET['num']

//Insert into DB $new_photo = array( array('id' => 1, 'name' => 'newphoto', 'unique' => uniqid()), array('id' => 2, 'name' => 'newphoto2', 'unique' => uniqid()) ); // select everything from db $photos = $new_photo; //display photos foreach($photos as $photo){ // display Link with $photo['unique'], <a href="link.php?num=$photo['unique']" var_dump($photo); } //GET Num // select where unique = $_GET['num']

因为我猜你真的不明白,所以我会成为一个好人并告诉你它是如何运作的。请务必记住,这只是一个不适合生产的例子。

创建2个PHP文件,test.php和test2.php

test.php的

运行一次,它将创建一个名为photos_temp的新表,它将插入2张照片。

// DB information
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "database";


// Create our photos_temp table
$test_query = "CREATE TABLE IF NOT EXISTS `photos_temp` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) NOT NULL,
        `unique` varchar(255) NOT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";

//Connect to DB
$conn = new mysqli($host, $username, $password, $dbname);

// Create Table
$table = $conn->prepare($test_query);
$table->execute();

// Data to insert into the database ( you can insert trough a form )
 $new_photos = array(
        array('name' => 'newphoto', 'unique' => uniqid()),
        array('name' => 'newphoto2', 'unique' => uniqid())
    );

 // Insert Query template
 $addquery = "INSERT INTO `photos_temp` (`name`, `unique`) VALUES (?, ?)";

 // foreach our data ( we will insert the 2 photos )
 foreach($new_photos as $photo){
            if($query = $conn->prepare($addquery)){
                    $query->bind_param('ss', $photo['name'], $photo['unique']);
                    $query->execute();
            }

 }

 echo 'finish inserting';

test2.php

这将显示照片并链接到您的独特照片。 // DB information $host = "localhost"; $username = "root"; $password = "password"; $dbname = "database"; // Create our photos_temp table $test_query = "CREATE TABLE IF NOT EXISTS `photos_temp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `unique` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"; //Connect to DB $conn = new mysqli($host, $username, $password, $dbname); // Create Table $table = $conn->prepare($test_query); $table->execute(); // Data to insert into the database ( you can insert trough a form ) $new_photos = array( array('name' => 'newphoto', 'unique' => uniqid()), array('name' => 'newphoto2', 'unique' => uniqid()) ); // Insert Query template $addquery = "INSERT INTO `photos_temp` (`name`, `unique`) VALUES (?, ?)"; // foreach our data ( we will insert the 2 photos ) foreach($new_photos as $photo){ if($query = $conn->prepare($addquery)){ $query->bind_param('ss', $photo['name'], $photo['unique']); $query->execute(); } } echo 'finish inserting';