我目前正在创建一个页面,该页面将显示编译到 MySQL 数据库中的随机图像和文本。到目前为止,我得到了这个:
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("mysite.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("db_name") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
表单可以正常工作,并将图片提交到 MySQL 数据库,同时提供图片和文本。
对每个变量使用 VARCHAR(30)。
我想知道如何着手创建一个 HTML/PHP 页面来显示来自该数据库的随机文本和随机图像。
如果你能帮上忙,请告诉我!
最佳答案
只需使用这个简单的代码:
<?php
$qry = mysql_query("SELECT * FROM employees ORDER BY RAND() LIMIT 1");
while($rand = mysql_fetch_assoc($qry)){
echo $rand['name'];
echo $rand['pic'];
}
?>
希望对您有所帮助!
关于javascript - 如何显示从 MySQL 数据库中获取的随机文本和图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368067/