Method for randomly obtaining Mysql Data

Source: Internet
Author: User

In the Mysql database, how can we obtain random Mysql Data in a table? The following describes how to obtain Mysql Data randomly.

 
 
  1. header("Content-type:text/html;charset=utf-8");     
  2. $mysql_host = 'localhost';     
  3. $mysql_un = 'root';     
  4. $mysql_pwd = '123456';     
  5. $my_db_name = 'mytestdb';     
  6. $my_table_name = 'mytesttable';     
  7. $output_str = '';     
  8. $resource = mysql_connect($mysql_host,$mysql_un,$mysql_pwd) or die('Mysql connect error!');     
  9. mysql_select_db($my_db_name,$resource);      
  10. $result = mysql_query("select id from $my_table_name",$resource) or die('select data error');     
  11. $my_id_array = array();     
  12. while($row = mysql_fetch_assoc($result)){     
  13.     if(is_numeric($row['id'])){     
  14.         $my_id_array[] = $row['id'];     
  15.     }     
  16. }     
  17. mysql_free_result($result);     
  18. if(count($my_id_array)>0){     
  19.     $rand_query_id = array_rand($my_id_array,1);     
  20.     $result = mysql_query("select * from $my_table_name where id = $rand_query_id",$resource) or die('select data error2');     
  21.     $row = mysql_fetch_assoc($result);     
  22.     $output_str = "
    {$row['foodname']} {$row['price']} {$row['tel']}
    ";     
  23.     mysql_free_result($result);     
  24. }else{     
  25.     $output_str = 'Without any data';     
  26. }     
  27. mysql_close($resource);     
  28. echo $output_str;     
  29. ?>   

The preceding describes how to obtain Mysql Data randomly.

Provides you with an in-depth understanding of Mysql temporary tables.

MySQL Show statement usage

How to clear a table in Mysql

Command for viewing the Mysql table structure

MySQL connection query Overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.