简体   繁体   English

在PHP中使用if / else检查SQL查询

[英]check a SQL query with if / else in PHP

I'm a complete novice in this but try to learn a lot by myself / tutorials. 我是一个完全的新手,但尝试自己/教程学习很多。 However I can not seem to find the answer to this (probably) easy problem. 但是,我似乎找不到这个(可能)简单问题的答案。 I want to check an output from my SQL database if it is smaller than 10 and then give me the current value of the INT. 我想检查我的SQL数据库的输出是否小于10,然后给我INT的当前值。 If it is not yet 10 or more then I want to output a line and if so output an other line. 如果不是10或更多,那么我要输出一行,如果要输出另一行。 However, it does not seem to work properly at the moment. 但是,它目前似乎无法正常工作。

//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'];

I assumed that you were trying to get the count of rows from the database (as others have done) and have left the answer for if that were the case below this one. 我假设您正在尝试从数据库中获取行数(就像其他人所做的一样),并且在这种情况下 ,答案是否定的。 You might find it useful and it seems like a waste to delete it! 您可能会发现它很有用,删除它似乎很浪费!

Anyway, judging from your comment above and the $cursus01['counter'] in your question I'm now going to assume that what you actually want is the following... 无论如何,从上面的评论和问题中的$cursus01['counter']来看,我现在假设您真正想要的是以下内容...

Solution

I suggest that you use the mysqli method below as mysql will at some point in the near future be removed from the language as it is deprecated! 我建议您使用下面的mysqli方法,因为不赞成在不久的将来将mysql从该语言中删除! If you continue to use it and create sites/apps with it then when this eventually happens you'll either have to use an old version of php which has it's own risks/limitations attached or face having a lot of broken code to fix... 如果您继续使用它并使用它创建站点/应用程序,那么当这种情况最终发生时,您将不得不使用旧版本的php附带其自身的风险/限制),或者面对很多无法修复的代码。 。

mysql 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的

$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
}



Original answer 原始答案

I assume that you are actually trying to echo a count of the number of rows returned? 我假设您实际上是在尝试echo返回的行数计数? Your current code echo sa count of the number of coulmns. 您当前的代码echo了库仑数。 If you don't know already it would suggest that there are bigger problems here.... 如果您还不知道,这表明这里存在更大的问题。

Solution

mysql 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的

$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
}

The way you're doing this, the count is going to be the same every time. 您这样做的方式,每次的计数都会相同。 You're count($cursus01) returns the number of columns in your database, to get the number of rows you should use the num_rows function or do a count query, unless you need the data from the query in which case you should do something like.... 您的count($ cursus01)返回数据库中的列数,要获取行数,您应该使用num_rows函数或执行一个计数查询,除非您需要查询中的数据,在这种情况下,您应该执行一些操作喜欢....

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

In this case count($cursus01) will return the number of rows. 在这种情况下,count($ cursus01)将返回行数。 Hope i didn't confuse what you were asking. 希望我不会混淆您的要求。 As edward pointed out you should start using mysqli instead of mysql. 正如爱德华指出的那样,您应该开始使用mysqli而不是mysql。 You also might want to look into using oop instead of procedural calls. 您可能还想研究使用oop而不是过程调用。 see the manual for an example HERE 见手册的例子该处

Agreed with Bryan answer, but when doing counts of database rows I'd choose the option of using MySQL count. 同意Bryan的回答,但是在对数据库行进行计数时,我会选择使用MySQL计数的选项。 In this case code line would be: 在这种情况下,代码行将是:

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

You will get the exact number of rows using this query 使用此查询,您将获得确切的行数

I assume you want to count the number of records stored in the table. 我假设您要计算表中存储的记录数。 use mysqli_num_rows() function instead of count(). 使用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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM