在其中一个表上应用condtion后,从整个数据库中选择

时间:2015-12-09 10:19:58

标签: php mysql

我有一个由$ tables中列出的10个表组成的数据库,

其中一个名为tbl_module的表包含一个名为delete_time的列,以下语句选择这些表中的所有数据:

$statement = "SELECT * FROM tbl_".$table;

我需要做的是添加一个条件来从这些表中选择所有数据 WHERE tbl_module中的delete_time值 IS NULL php代码是:

<?php
// alle relevanten Tabellen abfragen und als json zurückgeben.


$json["status"] = "running";
$details[] = "started get_tables ";
// Include confi.php
include_once('confi.php');
//var_dump($_POST);
$request_body = file_get_contents('php://input');
// first store the given set of data to keep it for future analysis
$statement = "INSERT INTO tbl_archive (content) VALUES ('$request_body' );";
mysql_query($statement);
$input = json_decode($request_body, true);
// now check if valid user
$user = $input["user"];

$username = $user["username"];
$password = $user["password"];

if($password and $username){


    $mySQLstring = "SELECT username, password, id FROM tbl_user where username = '$username' ;";
    $json["statement"][] = $mySQLstring;
    $qur = mysql_query($mySQLstring);
    //var_dump ( $qur );
    if ($qur){
        $res = mysql_fetch_assoc($qur);
    }
    if ($res){
        $json["res"] = $res;
        if ($res["password"] == $password){
            $json["username"] = $username;
            $json["id"] = $res["id"];
            $json["status"] = "ok";
            $tables = array("class", "class_user", "module", "module_class", "module_user", "rating", "student", "student_class");
            //$tables = array("class");
            foreach($tables as $table){
                $statement = "SELECT * FROM tbl_".$table;

                $qur = mysql_query($statement);
                if ($qur){
                    while($r = mysql_fetch_array($qur, MYSQL_ASSOC)){
                        //var_dump($r);
                        //echo (json_encode($r));
                        $result[$table][] = $r;
                    }
                }
            }
            $json = array("status" => "ok", "data" => $result);
        }
    }
}



@mysql_close($conn);

/* Output header */
header('Content-type: application/json');
echo json_encode($json);
?>

1 个答案:

答案 0 :(得分:0)

这很简单,如果$statement像这样

,则需要更改$table = module
        $tables = array("class", "class_user", "module", "module_class", 
                        "module_user", "rating", "student", "student_class");

        foreach($tables as $table){
            $statement = "SELECT * FROM tbl_".$table;

            if ( $table == 'module' ) {
                $statement .= ' WHERE delete_time IS NOT NULL ';

                // and to add not equal to 0000-00-00 00:00:00
                $statement .= " AND delete_time != '0000-00-00 00:00:00' ";
            }


            $qur = mysql_query($statement);
            if ($qur){
                while($r = mysql_fetch_array($qur, MYSQL_ASSOC)){
                    //var_dump($r);
                    //echo (json_encode($r));
                    $result[$table][] = $r;
                }
            }
        }

NOTE1:

  

请不要使用mysql_数据库扩展,不推荐使用(在PHP7中永远消失)   特别是如果您只是学习PHP,请花时间学习PDOmysqli_数据库扩展,   and here is some help to decide which to use

注2:

  

您在此代码中还有其他一些主要问题,主要是您直接使用$ _POST数组中的数据,而不对其进行任何验证或完整性检查,并将其直接放入查询中。您应该修复此问题,但使用PDO和命名或?位置参数将大大有助于缓解此问题。