2

我在这方面完全是新手,但尝试通过自己/教程学习很多东西。但是,我似乎无法找到这个(可能)简单问题的答案。我想检查我的 SQL 数据库的输出是否小于 10,然后给我 INT 的当前值。如果它还不是 10 或更多,那么我想输出一行,如果是,则输出另一行。但是,它目前似乎无法正常工作。

//Connect to DB
$con = mysqli_connect("localhost","root","root","cursuson");
//Get counter!
$getcounter = mysqli_query($con,"SELECT * FROM cursus01");
$cursus01 = mysqli_fetch_assoc($getcounter);

//check if output is less than 10
if(count($cursus01) <= 10)
    echo 'aantal aanmeldingen is ' . $cursus01['counter'] . '<br>';
else 
    echo 'De cursus gaat door! <br>';

//check the output
echo '<b>Aantal aanmeldingen:</b> ' . $cursus01['counter'] . '<br>'; 
echo '<b>ID:</b> ' . $cursus01['id'];
4

4 回答 4

2

我假设您正在尝试从数据库中获取行数(就像其他人所做的那样),并且已经留下了答案是否是这种情况。您可能会发现它很有用,删除它似乎是一种浪费!

无论如何,从你上面的评论和$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
}
于 2013-09-29T20:03:00.607 回答
0

你这样做的方式,每次计数都是一样的。您的 count($cursus01) 返回数据库中的列数,要获取应该使用 num_rows 函数或进行计数查询的行数,除非您需要查询中的数据,在这种情况下您应该做一些事情像....

$cursus01 = array();
while($row = mysql_fetch_assoc($getcounter))
    $cursus01[] = $row;

在这种情况下 count($cursus01) 将返回行数。希望我没有混淆你在问什么。正如爱德华指出的那样,您应该开始使用 mysqli 而不是 mysql。您可能还想研究使用 oop 而不是过程调用。有关示例,请参见手册HERE

于 2013-09-29T19:14:23.387 回答
0

同意 Bryan 的回答,但是在计算数据库行数时,我会选择使用 MySQL 计数的选项。在这种情况下,代码行将是:

$getcounter = mysql_query("SELECT COUNT(*) FROM cursus01");

您将使用此查询获得确切的行数

于 2013-09-29T19:54:28.673 回答
-1

我假设您想计算表中存储的记录数。使用 mysqli_num_rows() 函数而不是 count()。

$con = mysqli_connect("localhost","root","root","cursuson");
$getcounter = mysqli_query($con,"SELECT * FROM cursus01");
$row = array();//create an empty array
//check if output is less than 10
if(mysqli_num_rows($getCounter) <= 10):
  $row = mysqli_fetch_array($getCounter);
  echo $row['counter']."<br>";
else: 
 echo 'De cursus gaat door! <br>';
endif;

//display the output
echo '<b>Aantal aanmeldingen:</b> ' . $row['counter'] . '<br>'; 
echo '<b>ID:</b> ' . $row['id'];
于 2013-09-29T21:05:49.943 回答