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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教