我想知道如何为任意数字的照片id生成随机数,并为任意数字的用户id生成随机id,例如:
num =照片id idr =用户id
yourphoto.php?num=60&idr=3现在我想做的是生成一些东西,它不会告诉用户照片是什么id号码,或者用户id是随机的。
就像这样:
yourphoto.php?num=654654654648&idr=34546545621发布于 2014-01-14 01:14:26
查看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']
既然我猜你不是真的明白,我会做个好人,向你展示它是如何工作的。请务必记住,这只是一个不适合生产的示例。
创建两个PHP文件: test.php和test2.php
test.php
运行一次,它将创建一个名为photos_temp的新表,它将插入两张照片。
// 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";
$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);
}
https://stackoverflow.com/questions/21097289
复制相似问题