简体   繁体   中英

How to generate random id's

I Was wondering how can i generate a random number for the id of the photo being any number and generate random id for the users id being any number for example:

num = photo id idr = user id

yourphoto.php?num=60&idr=3

now what i want to do is generate something that doesn't tell the user what id number the photo is or user id is so something random.

Like That:

yourphoto.php?num=654654654648&idr=34546545621

Check uniqid , it will meet your needs.

Kinda pseudo code ( related to the comments )

  //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'] 

Since i guess you don't really understand, i'm going to be a nice guy and show you how it works. Make sure to remember this is just an example not good for production.

Create 2 PHP files, test.php and test2.php

test.php

Run this once and it will create a new table called photos_temp and it will insert 2 photos.

 // 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

This will display the photos and link to your unique photo.

 // 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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM