1

ブール値を返す関数を構築しようとしています。行のすべてのフィールドが空でない場合は TRUE を返す必要があり、空のフィールドがある場合は FALSE を返す必要があります。テーブルには多くのフィールドがあるので、PHP と MySQL で効率的に行う方法はありますか?

ありがとう!

編集:

私の現在のスクリプトは次のとおりです。

private function isFull(){
    $mysqli = //I create the connection
    $result = $mysqli->query("SELECT * FROM table WHERE id = 1 LIMIT 1");
    if($row = $result->fetch_array()){
        if($row['field1'] != ''){
            $toReturn = TRUE;
        } else {
            $toReturn = FALSE;
        }
        //etc
    }
}
4

3 に答える 3

3

行をループして値を確認し、空の行が見つかった場合は中断して false を返すことができます...

private function isFull(){
    $mysqli = //I create the connection
    $result = $mysqli->query("SELECT * FROM table WHERE id = 1 LIMIT 1");
    if($row = $result->fetch_array()){
        //assume that all are not empty and set the return value to true
        $return = true;
        //loop over each field in the row ...
        foreach($row as $key => $value){
            if(empty($value)){
               //at the first empty field, set return to false and break the loop
               $return = false;
               break;
            }
        }
    } else {
        //no row?
        $return = false;
    }

    return $return;
}
于 2013-08-29T15:38:54.793 に答える
1

シンプルにしない理由:

private function isFull(){
    $mysqli = //I create the connection
    $result = $mysqli->query("SELECT * FROM table WHERE id = 1 LIMIT 1");
    if($row = $result->fetch_array()){
        if(trim($row['field1']) == '' || trim($row['field2']) == ''){
            return false;
        }
    }
    return true;
}

いずれかのフィールドが空の場合、false が返されます。それ以外の場合は true を返します。

trim()、可能な end-space と startspace を削除するために使用されます。

結果を制限したくない場合は、whileループを使用してください。while ループ内で何かを返すと、while ループが中断されるため、残りは実行されません。

于 2013-08-29T15:39:04.067 に答える
0

私はするだろう:

$empty=true;
$qry = mysql_query($query,$connect_db);
while ($data = mysql_fetch_assoc($qry)){
    if($data["column"]=="")
    {
        $empty=false;
    }
}
于 2013-08-29T15:33:38.887 に答える