我有下面的代码,应该检查数据库条目与特定的用户名这是工作的,但当我尝试添加一些代码来检查返回的行是否大于10,然后运行一些代码来限制行数为10,然后如果不运行另一段代码将显示所有行。
代码: mysql_select_db("blahblah", $con); //connect to database..
$username = basename(dirname(__FILE__));
$username = mysql_real_escape_string($username);
$checkres = mysql_query("SELECT COUNT link, notes, titles, color FROM links WHERE username='" . $username . "';");
if ($checkres>10)
{
$resultsmall = mysql_query("SELECT link, notes, titles, color FROM links WHERE username='" . $username . "' LIMIT 10;");
while ($rowsmall = mysql_fetch_array($resultsmall)) { //loop
extract($rowsmall);
$htmlsmall .= "
//code goes here to format results
";
echo $htmlsmall; //display results...
}
}
else {
$result = mysql_query("SELECT link, notes, titles, color FROM links WHERE username='" . $username . "';");
while ($row = mysql_fetch_array($result)) { //loop
extract($row);
$html .= "
//code goes here to format results
";
echo $html; //display results...
}
}
mysql_close($con); //close db..
但是它只是显示数据库中两倍的行数,而不是限制它或显示所有行数。我该如何解决这个问题?//code goes here for formatting
并不重要,它只是格式化代码,使其看起来很漂亮,并显示在一个框中…
谢谢!
编辑:我现在有了这段代码,但它现在什么都不显示?有人能帮忙吗?
代码:mysql_select_db("blahblah", $con); //connect to database..
$username = basename(dirname(__FILE__));
$username = mysql_real_escape_string($username);
$sql = "SELECT SQL_CALC_FOUND_ROWS link, notes, titles, color FROM links WHERE username='" . $username . "' LIMIT 10";
$result = mysql_query($sql) or die("MySQL error: " . mysql_error());
$subsql = "SELECT found_rows() AS foundrows;";
$subresult = mysql_query($subsql) or die("MySQL error: " . mysql_error());
$found_rows = $subresult['foundrows'];
if($found_rows > 10)
{
while($row = mysql_fetch_array($result))
{
extract ($row);
$html .= "
//getting values from columns and then formatting them goes here
";
echo "Only 10 is allowed.";
}
}
else
{
while($row = mysql_fetch_array($result))
{
extract($row);
$html .= "
//getting values from columns and then formatting them goes here
";
}
}
mysql_close($con); //close db..
谢谢!
编辑2(2011年4月12日14:03 GMT):
我现在有了这段代码,这段代码得到了Col. Shrapnel的帮助,但它没有为我显示任何东西,我不知道为什么,请提供一些帮助。
代码:mysql_select_db("blahblah", $con); //connect to database..
$username = basename(dirname(__FILE__));
$username = mysql_real_escape_string($username);
$sql = "SELECT link, notes, titles, color FROM links WHERE username='$username' LIMIT 10";
$res = mysql_query($sql);
if (mysql_num_rows() == 10) {
while ($row = mysql_fetch_array($res)) {
extract($row);
$htmlsmall .= "
=";$htmlfree .= "只允许10个";echo $ htmlsmall;echo $ htmlfree;}}其他{While ($row = mysql_fetch_array($res)) {提取($ rowsmall);$htmlsmall .= "
";
echo $htmlsmall;
}
}
现在,如果我查看页面源代码,我可以看到这两个错误:
<b>Warning</b>: Wrong parameter count for mysql_num_rows() in <b>//url was here</b> on line <b>109</b><br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>//url to file goes here</b> on line <b>125</b><br />
第109行是:if (mysql_num_rows() == 10) {
第125行是:while ($row = mysql_fetch_array($res)) {
(这些行在上面的代码中)
请问有人能帮我一下吗?谢谢!
mysql_query()
返回结果语句句柄,如果查询失败则返回布尔值FALSE。外部查询无效:
SELECT COUNT ...
不起作用,因此查询失败,返回false,并跳转到较低的"unlimited"查询。
但是,您正在以一种迂回的方式进行限制,这迫使查询至少运行两次。
你想要的是
$sql = "SELECT SQL_CALC_FOUND_ROWS link, notes, etc.... LIMIT 10";
$result = mysql_query($sql) or die("MySQL error: " . mysql_error());
$subsql = "SELECT found_rows() AS foundrows;";
$subresult = mysql_query($subsql) or die("MySQL error: " . mysql_error());
$found_rows = $subresult['foundrows'];
if ($found_rows > 10) {
... there's more than 10 rows available, though we're fetching only 10 because of the LIMIT
} else {
... 10 rows or less
}
SQL_CALC_FOUND_ROWS
是一个MySQL扩展,它强制MySQL计算在没有LIMIT子句的情况下将获取多少行。您可以使用found_rows()
查询函数获得此计算的结果。
这样,您只需运行一次主查询,执行非常非常快的found_rows()查询,然后就可以开始了。
考虑到除了"only 10 allowed"行外,"大于10"answers"小于等于10"两种版本的输出格式似乎没有任何不同,那么下面这行如何:
$sql = "...";
$result = mysql_query($sql) or die(mysql_error());
$rowcount = 0;
while ($row = mysql_fetch_assoc($result)) {
$rowcount++;
... output a row ...
if ($rowcount > 10) {
echo "Only 10 allowed";
break;
}
}
不需要重复的代码,多余的逻辑等…
我尝试添加一些代码来检查返回的行是否大于10,然后运行一些代码来限制行数为10
这没有意义。
只要用LIMIT 10运行你的查询,你就会得到你的数据。
mysql_select_db("blahblah", $con); //connect to database..
$username = basename(dirname(__FILE__));
$username = mysql_real_escape_string($username);
$sql = "SELECT * FROM links WHERE username='$username' LIMIT 10";
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
while ($row = mysql_fetch_array($res)) {
extract($rowsmall);
$htmlsmall .= ""; //code goes here to format results
}
echo $htmlsmall;
。如果仍然想要回显"Only 10 is allowed"。(不知道为什么)你可以在
下面添加这3行if (mysql_num_rows($res) == 10) {
echo "Only 10 is allowed.";
}
但是我看不出有什么意义。
还要注意您的程序设计可能是错误的,因为您显然是在为系统中的每个用户复制这个文件
要获取结果中返回的行数,必须使用mysql_num_rows.
if (mysql_num_rows($checkres)>10)