没有从 SELECT 查询中获得预期的结果
我试图从一个有 16k 行的表中获取 10 个结果.
I'm trying to grab 10 results from a table that has 16k rows.
此表中有一行称为 views 每次查看艺术家时都会获得 +1.但只有顶级艺术家才能得到意想不到的结果.我将视图行编入索引以提高速度.我知道我必须循环它,但我不确定如何获得所有 10 行
With in this table is a row called views that gets a +1 each time an artist is viewed. But I'm getting unexpected results only the top artist. I have the views row indexed for speed. I know I have to loop it, but I'm unsure how to get all 10 rows
我没有处理过循环或一次获得多于一行的问题,需要帮助使用新的 mysqli 对其进行格式化
I haven't dealt with looping or getting more than one row at a time and need help formatting it with the new mysqli
// Get Top Viewed Artist
$TopViewedArtist = mysql_query("SELECT * FROM `artists` ORDER BY `artists`.`views` DESC LIMIT 10");
$TopViewedArtistInfo = mysql_fetch_assoc($TopViewedArtist);
$TopViewedArtist_TotalRows = mysql_num_rows($TopViewedArtist);
print_r($TopViewedArtistInfo); // the data returned from the query
这是一种以可读格式显示艺术家姓名结果的解决方案.
This is the solution to display the results of the artist name in a readable format.
$TopViewedArtists = mysql_query('SELECT * FROM `artists` ORDER BY `artists`.`views` DESC LIMIT 10');
while (($TopArtist = mysql_fetch_assoc($TopViewedArtists)) !== FALSE) {
//print_r($TopArtist);
echo $TopArtist['artist']; echo "<br>";
}
此代码可以为其他人更改.但需要更新到mysqli
This code can be change for others. but needs to be updated to mysqli
mysql_query
返回一个 result
资源对象.把它想象成一个数组.读取该数组内容的唯一方法是遍历它.将 mysql_fetch_assoc
视为与数组的 each
相同:它返回下一行并递增内部指针.我认为这就是您想要做的:
mysql_query
returns a result
resource object. Think of it as an array. The only way to read the contents of that array is to iterate through it. Think of mysql_fetch_assoc
as the same as each
for arrays: it returns the next row and increments the internal pointer. I think this is what you want to do:
<?php
// Get Top Viewed Artist
$TopViewedArtists = mysql_query('SELECT * FROM `artists` ORDER BY `artists`.`views` DESC LIMIT 10');
while (($artist = mysql_fetch_assoc($TopViewedArtists)) !== FALSE) {
print_r($artist);
}
?>
还要记住 mysql_fetch_assoc
返回一个包含多个值的数组.该数组应该包含您看到的所有内容;使用 $artist['artist']
访问值(输出 Gwen Stefani
).
Keep in mind also that mysql_fetch_assoc
returns an array with multiple values. The array is supposed to contain everything you see; access the values with $artist['artist']
(outputs Gwen Stefani
).
我可以建议你研究 mysqli 或 PDO 而不是基本的 mysql 函数吗?使用 mysql_
函数的唯一原因是如果你坚持使用 PHP 4(不再支持,所以没有人应该仍然使用它)或旧应用程序.这看起来两者都不是,而且您似乎对界面没有任何现有经验,因此您确实应该寻找更好的选择.
May I suggest that you look into mysqli or PDO instead of the basic mysql functions? The only reason to use mysql_
functions is if you're stuck with PHP 4 (no longer supported so nobody should still be using it) or old applications. This looks like neither, and as it also seems you have no existing experience with the interface you really ought to look into the better options.