我假设您正在尝试从数据库中获取行数(就像其他人所做的那样),并且已经留下了答案是否是这种情况。您可能会发现它很有用,删除它似乎是一种浪费!
无论如何,从你上面的评论和$cursus01['counter']
你的问题来看,我现在假设你真正想要的是以下......
解决方案
我建议您使用mysqli
下面的方法,因为它mysql
会在不久的将来从语言中删除,因为它已被弃用!如果您继续使用它并使用它创建站点/应用程序,那么当最终发生这种情况时,您要么必须使用php
具有自身风险/限制的旧版本,要么面临要修复大量损坏的代码......
mysql
mysql_connect("localhost","root","root");
mysql_select_db("cursuson");
$query = mysql_query("SELECT * FROM cursus01");
if(mysql_num_rows($query)){
//Rows are returned
while($row = mysql_fetch_assoc($query)){
//Do stuff with row data
if($row['counter'] <= 10){
//Counter is less than or exactly 10
}
else{
//Counter is greater than 10
}
}
}
else{
//No rows were returned
}
mysqli
$mysqli = new mysqli('localhost', 'root', 'root', 'cursuson');
$result = $mysqli->query("SELECT * FROM cursus01");
if($result->num_rows;){
//Rows are returned
while($row = $result->fetch_assoc()){
//Do stuff with row data
if($row['counter'] <= 10){
//Counter is less than or exactly 10
}
else{
//Counter is greater than 10
}
}
}
else{
//No rows were returned
}
原始答案
我假设您实际上是在尝试echo
计算返回的行数?您当前的代码echo
是对 coulmns 数量的计数。如果您还不知道,这表明这里存在更大的问题....
解决方案
mysql
mysql_connect("localhost","root","root");
mysql_select_db("cursuson");
$query = mysql_query("SELECT * FROM cursus01");
$count = mysql_num_rows($query);
if($count <= 10){
//Less than (or exactly) 10 rows were returned
while($row = mysql_fetch_assoc($query)){
//Do stuff with row data
}
}
else if($count > 10){
//More than 10 rows were returned
while($row = mysql_fetch_assoc($query)){
//Do stuff with row data
}
}
else{
//No rows were returned
}
mysqli
$mysqli = new mysqli('localhost', 'root', 'root', 'cursuson');
$result = $mysqli->query("SELECT * FROM cursus01");
$count = $query->num_rows;
if($count <= 10){
//Less than (or exactly) 10 rows were returned
while($row = $result->fetch_assoc()){
//Do stuff with row data
}
}
else if($count > 10){
//More than 10 rows were returned
while($row = $result->fetch_assoc()){
//Do stuff with row data
}
}
else{
//No rows were returned
}