我无法从PHP中的MySQL查询中获取行计数。
test page和PHP代码的链接如下:
<?php
$con=* TESTED AND WORKING *
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$retDiv = "";
$counter = 0;
$result = mysqli_query($con,"SELECT * FROM `store` ORDER BY `num` DESC ");
$num_rows = mysql_num_rows($result);
while($row = mysqli_fetch_array($result))
{
if ($counter>=5)
{
break;
}
else
{
$retDiv = "<div id=\"hs" . $row['num'] . "\" class=\"store-item " . $row['Artist'] . "\">\n";
$retDiv = $retDiv . "<div id=\"". $row['Release'] . "-title\" class=\"album-title\">\n";
$retDiv = $retDiv . "<h1 align=\"center\" style=\"text-transform:uppercase;\">" . $row['Release'] . "</h1></br>\n";
$retDiv = $retDiv . "<h3 align=\"center\">" . $row['ReleaseName'] . "</h3>\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div id=\"" . $row['Release'] . "-description\" class=\"description\">\n";
$retDiv = $retDiv . "<table width=\"100%\">\n";
$retDiv = $retDiv . "<td width=\"50%\" style=\"padding-left:57px;padding-right:57px;padding-top:57px;padding-bottom:20px;\">\n";
$retDiv = $retDiv . "<a id=\"" . $row['Release'] . "DivLink\" href=\"javascript:;\" onmousedown=\"toggleDivStore('" . $row['Release'] . "-expand','" . $row['Release'] . "DivLink','" . $row['Release'] . "','" . $row['Release'] . "b','" . $row['Release'] . "')\"><img name=\"" . $row['Release'] . "-art\" src = \"images/" . $row['Release'] . ".jpg\" class=\"album-art\" onmouseover=\"this.src='images/" . $row['Release'] . "b.jpg'\" onmouseout=\"this.src='images/" . $row['Release'] . ".jpg'\"></a>\n";
$retDiv = $retDiv . "<td width=\"50%\" style=\"padding:20px;padding-top:0px;padding-bottom:0px;padding-left:0px;\">\n";
$retDiv = $retDiv . "<p style=\"\">" . $row['Description'] . "</p>\n";
$retDiv = $retDiv . "</td>\n";
$retDiv = $retDiv . "</table>\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div class=\"album-expand\" id=\"RELEASE-expand\" style=\"display:none;\">\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div =\"" . $row['Release'] . "PaypalContainer\" class=\"album-paypal-container\">\n";
$retDiv = $retDiv . $row['Paypal1'];
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "</div>\n";
echo $retDiv;
}
$counter = $counter+1;
}
echo "<div id=\"retNum\"><script type=\"text/javascript\">numReleases =" . $num_rows . ";numPages=Math.ceil(numReleases/5)-1;</SCRIPT></div>";
mysqli_close($con);
如何将行计数$num_rows
返回到retNum
Javascript?
答案 0 :(得分:0)
你正在混合mysqli(注意i
)和mysql(注意i的 LACK )。这两个库和函数体是 NOT 可互操作:
$num_rows = mysql_num_rows($result);
^--- missing an `i` here
答案 1 :(得分:0)
当您使用mysqli_query()方法从数据库中获取结果集时,可以通过调用mysqli_num_rows()方法获取结果中的行数,该方法将mysqli_result设置为参数,并且不使用mysql_num_rows( )。查看mysqli_num_rows()
答案 2 :(得分:0)
为什么不在你的sql查询中添加“LIMIT 5”而不是使用PHP逻辑?