首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何生成随机id

问如何生成随机id
EN

Stack Overflow用户
提问于 2014-01-14 01:13:39
回答 1查看 476关注 0票数 0

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

num =照片id idr =用户id

代码语言:javascript
复制
yourphoto.php?num=60&idr=3

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

就像这样:

代码语言:javascript
复制
yourphoto.php?num=654654654648&idr=34546545621
EN

回答 1

Stack Overflow用户

发布于 2014-01-14 01:14:26

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

一种伪代码(与评论相关)

代码语言:javascript
复制
   //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']

​

既然我猜你不是真的明白,我会做个好人,向你展示它是如何工作的。请务必记住,这只是一个不适合生产的示例。

创建两个PHP文件: test.php和test2.php

test.php

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

代码语言:javascript
复制
// 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

这将显示照片并链接到您独特的照片。

代码语言:javascript
复制
// DB information
$host = "localhost";
$username = "root";
$password = "password";
$dbname = "database";


$conn = new mysqli($host, $username, $password, $dbname);
$stmt = $conn->stmt_init();
$stmt->prepare('Select * from photos_temp');
$stmt->execute();


$result = $stmt->get_result();
while ($data = $result->fetch_assoc())
{
    echo '<a href="?num='.$data['unique'].'">'.$data['name'].'</a><br/>';
}

if(isset($_GET['num'])){
    $num = $_GET['num'];

    $stmt->prepare("SELECT * FROM `photos_temp` WHERE `unique` = ?");
    $stmt->bind_param("s", $num);
    $stmt->execute();

    $result = $stmt->get_result();

    $photo = $result->fetch_assoc();

    echo '<pre>';
    print_r($photo);
}

​

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21097289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档