SELECT * FROM多列

时间:2015-12-26 17:24:48

标签: php mysql

我是创建数据库的绝对初学者 我只知道我们可以使用,

"SELECT * FROM users WHERE username = '$username' and password = '$password'";

但是,如果我的SQL中有多个表并且我想全部选择它们怎么办?

我可以这么做吗,

"Select * from users where username = '$username' and password = '$password' and email = '$email' and address = '$address' and phone = '$phone'";

以下是我的PHP脚本:

public function does_user_exist($username,$password,$email,$address,$phone){
    $query = "Select * from users where username = '$username' and password = '$password' and email = '$email' and address = '$address' and phone = '$phone'";
        $result = mysqli_query($this->connection, $query);
        if(mysqli_num_rows($result) > 0){
            $json['success'] = 'Welcome '.$email;
            echo json_encode($json);    
            mysqli_close($this->connection);
            } else {
                $query = "Insert into users(username, password, email, address, phone) values ('$username','$password','$email', '$address', '$phone')";
                $is_inserted = mysqli_query($this->connection, $query);
                if ($is_inserted == 1){
                    $json['success'] = 'Account created, welcome '.$email;
                    } else {
                        $json['error'] = 'Wrong password ';
                        }
                        echo json_encode($json);
                        mysqli_close($this->connetion);
                }
        }

更新

<?php

require_once 'connection.php';
header('Content-Type: application/json');

class User {
    private $db;
    private $connection;

    function __construct() {
    $this->db = new DB_Connection();
    $this->connection = $this->db->get_connection();
    }

    public function does_user_exist($username,$password,$email,$address,$phone){
        $query = ("Select * from users where username = '$username' and password = '$password' and email = '$email' and address = '$address' and phone = '$phone'");
        $result = mysqli_query($this->connection, $query);
        if(mysqli_num_rows($result) > 0){
            $json['success'] = 'Welcome '.$email;
            echo json_encode($json);    
            mysqli_close($this->connection);
            } else {
                $query = "Insert into users(username, password, email, address, phone) values ('$username','$password','$email', '$address', '$phone')";
                $is_inserted = mysqli_query($this->connection, $query);
                if ($is_inserted == 1){
                    $json['success'] = 'Account created, welcome '.$email;
                    } else {
                        $json['error'] = 'Wrong password ';
                        }
                        echo json_encode($json);
                        mysqli_close($this->connetion);
                }
        }
}

$user = new User();
if (isset($_POST['username'], $_POST['password'], $_POST['email'], $_POST['address'], $_POSt['phone'])){

    $username = $POST['username'];
    $password = $_POST['password'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $phone = $_POST['phone'];


    if(!empty($username) && !empty($password) && !empty($email) && !empty($address) && !empty($phone)){
        $encrypted_password = md5($password);
        $user -> does_user_exist($username,$encrypted_password,$email,$address,$phone);
    } else {
        echo json_encode("You must fill all fields!")
        }
}

&GT;

希望你们能帮忙,我真的很感激答案。

2 个答案:

答案 0 :(得分:1)

您必须使用JOIN Queries。请尝试以下SQL Statement

SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;

请参阅此JOIN SQL SO Answers

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

Difference in MySQL JOIN vs LEFT JOIN

Mysql join query

答案 1 :(得分:0)

一般来说,您需要使用JOIN来执行此操作,并且您需要在表之间具有共同的值,这些值充当foreign keys,在其他表中引用相应的值。

修改

对问题和评论的更新表明原始问题略有错误。是的,您绝对可以在单个查询中引用/使用/比较/评估众多列。您发布的示例就是一个很好的例子:

SELECT (column1, column2) FROM users WHERE username = $username AND email = $email

依此类推,因为表格的列数很多。您还可以使用OR运算符,其效果是在电子邮件(或您喜欢的任何其他列)上包含与用户名OR匹配的任何行。