duanqianhuan3994 2017-02-27 09:08
浏览 75
已采纳

只使用php从phpmyadmin表中获取单行

I' m using the following code to get the desired details,that is, all the books that have the highest total count in order to show the most trending/popular books present in a website. The total column depicts the no. of times a particular book had been viewed by an user of the website. Here is the code`

<?php    
include("connection.php");
global $con;
$sql1= "SELECT book FROM vi_views ORDER BY total DESC LIMIT 3";
$query1= mysqli_query($con,$sql1);
if (!$query1)
{
  printf("Error: %s
", mysqli_error($con));
  exit();
}
$obj1=mysqli_fetch_array($query1,MYSQLI_BOTH);
$row1=mysqli_num_rows($query1);
if ($row1>0)
{
  echo var_dump($obj1);
}
?>

Here is a look of how my table looks

Table:vi_views

id  book  course  date  total

1   book1  null  dd//mm/yy 3   
2   null   course1 dd/mm/yy 1    
3  book2   null dd/mm/yy 8    

and so on....

The problem is that the query is working but only the first record according to the query is shown, that is, book2 in this case and no more books are showing. where I'm lacking in my code please tell...

  • 写回答

5条回答 默认 最新

  • dpzo13732 2017-02-27 09:20
    关注

    Try this:

    $sql1= "SELECT book FROM vi_views ORDER BY total DESC LIMIT 3";
    $query1= mysqli_query($con,$sql1);
    
    while ($row = mysqli_fetch_assoc($query1)) {
        print_r($row);
    }
    

    or

       while ($row = mysqli_fetch_assoc($query1)) {
        //print_r($row);
        $book=$row['book'];echo "<br/>";
        echo $book;
    }
    

    output:

    book1

    book2

    book3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等