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

时间:2016-02-22 04:26:58

标签: php mysql

我有几行代码没有按照我的预期给出答案。

请帮我写下最好的方法:

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

我希望如果 count = 1 goto enzelse (count is 0)继续其他计划

6 个答案:

答案 0 :(得分:4)

您可以尝试以下代码:

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

答案 1 :(得分:4)

尝试这种方式:

# change directory to location of copy
mkdir NEW-PROJECT.git
cd NEW-PROJECT.git

# create a local copy of the repository
git clone --bare https://username@bitbucket.org/username/OLD-PROJECT.git

# now create an empty repository on BitBucket called NEW-PROJECT

# push your entire code base to this new repository
git push --mirror https://username@bitbucket.org/username/NEW-PROJECT.git

答案 2 :(得分:3)

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

答案 3 :(得分:2)

所有其他答案都是正确。但检查错误也是一种很好的做法。

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

希望它会对你有所帮助:)。

答案 4 :(得分:0)

试试这种方式

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

答案 5 :(得分:0)

如果您使用 mysqli 数据库驱动程序,请输入以下代码

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

如果你使用简单的 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
}