简体   繁体   English

希望COUNT在PHP MYSQL中返回0或1

[英]Want COUNT to return a 0 or 1 in PHP MYSQL

I have a few lines of code that's not giving me answer as I expect. 我有几行代码没有像我期望的那样给我答案。

Please help me to write the best way for it: 请帮我写出最好的方法:

if ("SELECT COUNT (*) FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1"){
    if (1) goto enz;
}

I want if count = 1 goto enz , else (count is 0) and continue with rest of program 我想要if count = 1 goto enzelse (count is 0)继续执行程序的其余部分

You can try this code: 您可以尝试以下代码:

function getConnected($host, $user, $pass, $db)
{

    $mysqli = new mysqli($host, $user, $pass, $db);
    mysqli_set_charset($mysqli, 'utf8');

    if ($mysqli->connect_error)
        die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());

    return $mysqli;
}

$mysqli = getConnected($db_host, $db_user, $db_password, $db_name);
$sql = "SELECT COUNT (*) as count FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1";

if ($result = $mysqli->query($sql)) {
    while ($obj = $result->fetch_object()) {
        if($obj->count == "1"){
          //Enter you code here to goto enz part 
        } else {
          //Continue with rest of program
        }
    }
}

Try in this way : 以这种方式尝试:

$sql = "SELECT COUNT (*) as count FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1";

if ($result = mysqli_query($con,$sql)) {
    while ($row = mysqli_fetch_assoc($result)) {
        if($row['count'] == "1"){
          //Enter you code here to goto 'enz'
        } else {
          //Continue with rest of program
        }
    }
}
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sql="SELECT COUNT (*) as cnt FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1";
$result=mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
if ($row['cnt'] == 1) {
//Do your code (goto enz)
} else if ($row['cnt'] == 0) {
//Continue rest
}

All other answers are Correct . 所有其他答案都是正确的 But it is also a good practise to check for Errors . 但是,检查错误也是一个好习惯。

$con = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$sql = "SELECT COUNT (*) as count FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1";
$result = mysqli_query($con,$sql));

// Check for errors
if(!$result){
   printf("Error: %s\n", mysqli_error($con));
}

while ($row = mysqli_fetch_assoc($result)) {
    if($row['count'] == "1"){
      // Success. Write your code for redirection
    }else{      
      //Continue with rest of program
    }
}

Hope it will help you :) 希望它能对您有所帮助:)

Try this way 试试这个

$sql    = 'SELECT COUNT (*) as foo FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1';
$result = mysql_query($sql, $link);

if($row['foo']>1) {
 goto enz;
}

If you are using mysqli DB DRIVER then put following code 如果您使用的是mysqli DB DRIVER,则输入以下代码

$query = "SELECT COUNT (*) as total FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1";

if ($result = mysqli_query($con,$query)) {
    while ($row = mysqli_fetch_assoc($result)) {
        if($row['total'] == "1"){
          //Make your code here
        } else {
          //Make rest of the code
        }
    }
}

And If you are using simple mysql DB DRIVER then 如果您使用的是简单的mysql DB DRIVER,

$query = 'SELECT COUNT (*) as total FROM completed WHERE userid = $memid AND filetid = $fileid1 LIMIT 1';
$result = mysql_query($query, $link);
$row    = mysql_fetch_array($result)

if($row['total'] == "1"){
    //Make your code here
} else {
    //Make rest of the code
}

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

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