Sql Query没有在模型中返回数组结果?

时间:2017-08-20 13:58:01

标签: php database mysqli model

在我的滚动Feed模型中,我返回数据库中的用户数,并且最后50位用户按升序添加到数据库中。出于某种原因,最后50位用户没有返回 我的型号代码如下:

<?php

//display total records in db


//add html
echo '<link rel="stylesheet" href="assets/css/feed.css" /><div id="feed"<marquee>
<B>Available Accounts: </B>';

//fetch amount of users
$usercount="SELECT user_name FROM store";
if ($res=mysqli_query($conn,$usercount))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($res);
  printf("%d",$rowcount);
  // Free result set
  mysqli_free_result($res);
  } 

 //add html 
 echo 
  '<b>' . 
  ' . . . Last 50 Added Usernames . . .</b>';
//last 50 added to the database
$lastusers = mysqli_query
  ("SELECT user_name FROM (
  SELECT * 
  FROM store 
  ORDER BY user_id DESC
  LIMIT 50
) AS store ORDER BY user_id ASC");

$row = mysqli_fetch_array($lastusers);
echo $row['user_name'];

echo '</marquee></div>';


?>

1 个答案:

答案 0 :(得分:3)

试一试!

<?php


//MySQLi information

$db_host     = "localhost";
$db_username = "username";
$db_password = "password";

//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());

//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());


//fetch amount of users
$usercount = mysqli_query($connection, "SELECT user_name FROM store");
$rows      = mysqli_num_rows($usercount);
echo $rows . " Users!" . "<br>";


//last 50 added to the database
$row = mysqli_query($connection, "SELECT * FROM store LIMIT 50");

while ($lastusers = mysqli_fetch_array($row)) {
    echo $lastusers['user_name'] . "<br>";
}

测试并且工作正常,这是一个例子! enter image description here

祝你好运!

相关问题