简体   繁体   English

通过PHP从MySQL中随机选择一条记录

[英]Randomly Selecting A Record From MySQL Through PHP

I know that there's a lot of question in here that already solved that are about my question; 我知道这里已经解决了很多有关我的问题的问题; however, I am trying to implement it through a link, and I can't seem to make it work. 但是,我正在尝试通过链接来实现它,但似乎无法使其正常工作。

I have a table named tbData , and columns within it that's named firstName & lastName ; 我有一个名为tbData的表,表中的列名为firstNamelastName I am trying to select a random record off of it and I'm using the following code: 我正在尝试从中选择随机记录,并且正在使用以下代码:

JavaScript: JavaScript的:

<script type="text/javascript" src="jquery.min.js"></script>
   <script type="text/javascript">
        function randomfunction() {
            $.get("random.php");
            return false;
        }
</script>

HTML: HTML:

<span style="color:#F0E6C3; font-size:30px; font-family: Trajan Pro;">                          
    <a href="#" onclick="randomfunction();">RANDOMIZER!</a>
</SPAN>

PHP: PHP:

<?php
    $con=mysqli_connect("localhost","root","","dbPos");
        if (mysqli_connect_errno($con))
            {
                echo "Failed to connect to MySQL " . mysqli_connect_error();
            }

        $random = mysqli_query($con,"SELECT * FROM tbData order by RAND() LIMIT 1");
            $row = mysqli_fetch_array($random);
                while ($row = mysql_fetch_array($random))
                    {
                      echo $row['firstName'];
                mysqli_close($con);
?>

I'm trying to display the firstName and lastName as a string below the RANDOMIZER! 我正在尝试将firstName和lastName显示为RANDOMIZER下面的字符串 link on my HTML file, but upon clicking it, nothing happens; 我的HTML文件上的链接,但是单击它没有任何反应; am I missing something? 我错过了什么吗?

Since you limit 1 your mySQL request, remove your while { } and just echo $row['firstName']; 由于限制了1个mySQL请求,因此删除while {},然后echo $row['firstName']; should work 应该管用

<?php
        $con=mysqli_connect("localhost","root","","dbPos");

        if (mysqli_connect_errno($con)) {
                echo "Failed to connect to MySQL " . mysqli_connect_error();
        }

       $random = mysqli_query($con,"SELECT * FROM tbData order by RAND() LIMIT 1");

       $row = mysqli_fetch_array($random);      
       echo $row['firstName'];
       mysqli_close($con);
?>

For the html/jquery part as following : 对于html / jquery部分,如下所示:

<!doctype html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<span style="color:#F0E6C3; font-size:30px; font-family: Trajan Pro;">                          
    <a href="#" class=randomizer>RANDOMIZER!</a>
</span>
<p class=result></p>
<script>
$(".randomizer").click(function(){
  $.get("random.php", function(data, status){
    $('.result').html(data);
  });
});
</script>
</body>
</html>

Here you go. 干得好。

You should have a call back in your .$get() as such: 您应该在.$get()中这样调用:

$.get('random.php', (results) => {
   // results will equal $row['firstName'] now 
});

If you need both values, I'd recommend changing your echo to 如果您需要两个值,建议您将回显更改为

echo json_encode($row);

And then use JSON.parse(results) in your get callback 然后在get回调中使用JSON.parse(results)

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

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