简体   繁体   English

如何获取mysql数据库ID以随机回显?

[英]How do I get mysql database id to echo randomly?

I have created a PHP/ mysql search database following a tutorial online. 我已经按照在线教程创建了一个PHP / mysql搜索数据库。 everyhthing is working great. 一切都很好。 I just want to know how I can get the results within the database to display in a random order. 我只想知道如何在数据库中获取结果以随机顺序显示。

currently the results are displaying in a numerical order according to its ID number within the mysql database. 当前,结果根据其在mysql数据库中的ID号以数字顺序显示。 I want to know how to display results randomly? 我想知道如何随机显示结果吗?

The id is called upon using $id. 使用$ id调用ID。 here is the PHP: 这是PHP:

<?php

$button = $_GET ['submit'];
$search = $_GET ['search'];

if(strlen($search)<=1)
echo "Search term too short";
else{
echo "<h1>Dislpaying results for <b>$search</b> <hr size='1'></h1></br>";
mysql_connect("localhost","dobhgc767545ch2","jhjhghtyutuhjh");
mysql_select_db("doorcouhjhjh2");

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="OR keywords LIKE '%$search_each%'";

}

$constructs ="SELECT * FROM search WHERE $construct";
$run = mysql_query($constructs);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{

echo "$foundnum results found !<p>";

$per_page = 9;
$start = $_GET['start'];
$max_pages = $foundnum / $per_page;
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM search WHERE $construct LIMIT $start, $per_page");

while($runrows = mysql_fetch_assoc($getquery))
{
$title = $runrows ['title'];
$description = $runrows ['description'];
$link = $runrows ['link'];
$image = $runrows ['image'];


echo "
<div class=\"pagination\" style=\"display:inline\"><ul style=\"background-color:#\"><li><div      class=\"span3_search\"><div class=\"title_bg\"><h2><a href='$link'><b>$title</b>    </a></h2></div><div class=\"result_img\"><a href='$link'><img id=\"result_img\" src=\"$image\" /></a>    </div><div class=\"result_desc\"><p>$description</p></div><div class=\"result_link\"><br />
<a href='$link'>$link<br /><br /></a><p></div></div></li></ul></div>
";


?>

if it is a small data set you can just use mysql's 如果它是一个小的数据集,您可以使用mysql

ORDER by RAND()

don't do this on a large data set, it is very inefficient 不要在大型​​数据集上执行此操作,效率非常低

$constructs ="SELECT * FROM search WHERE $construct ORDER BY rand()";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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