将condtion应用于表数组

时间:2015-12-09 00:55:24

标签: php mysql json

我有以下PHP文件,我需要将where条件添加到条件应该如下的select语句中:WHERE tbl_module.delete_time IS NUll 数据库中的表以tbl_开头,而数组中没有tbl_

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

您应该查看Official MySQL Documentation

SELECT
[ALL | DISTINCT | DISTINCTROW ]
  [HIGH_PRIORITY]
  [MAX_STATEMENT_TIME = N]
  [STRAIGHT_JOIN]
  [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
  [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
  [PARTITION partition_list]
[WHERE where_condition]

所以WHERE条件应该在FROM之后。您应该能够轻松修改$statement变量以包含它。