简体   繁体   English

通过连接两个表获取数据:PHP

[英]Fetch Data by connecting two tables : PHP

I am trying t fetch the user_email based on the foreign Key I have set in the other table.我正在尝试根据我在另一个表中设置的外键获取 user_email。

Suppose in one Table 'Users'I Have:假设在一张表“用户”中我有:

Users: id, user_email, user_phone etc.用户:id、user_email、user_phone 等。

and I Have other Table named patients as我还有其他表将患者命名为

patient_id, patient_name, doctor_id. patient_id,patient_name,doctor_id。

Where doctor_id is the foreign key to the Users Table.其中 doctor_id 是用户表的外键。

I have the following code:我有以下代码:

$query = "SELECT 'users.user_email' as 'name'
FROM users,patients 
WHERE 'users.id'='patients.doctor_id' AND 'users.id'='1';
" ;
 $result = mysqli_fetch_assoc($conn,$query) ;

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] ;
}

THe problem is its not working.问题是它不起作用。 its giving an error:它给出了一个错误:

Warning: mysqli_fetch_assoc() expects exactly 1 parameter, 2 given in 
C:\xampp\htdocs\joli\data\foreigndata.php on line 10

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null 
given in C:\xampp\htdocs\joli\data\foreigndata.php on line 12

PS: The connection to the database is already established. PS:与数据库的连接已经建立。

You should use mysqli_query() to run the query and then mysqli_fetch_assoc() to get the result as used below:您应该使用mysqli_query()来运行查询,然后mysqli_fetch_assoc()来获取结果,如下所示:

$query = "SELECT users.user_email as name
FROM users,patients 
WHERE users.id=patients.doctor_id AND users.id=1;
" ;

$result=mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] ;
}

Try this试试这个

select users.user_email as name from users inner join patients on users.id=patients.doctor_id where users.id='1'选择 users.user_email 作为 users inner join patients on users.id=patients.doctor_id where users.id='1'

Try this试试这个

$query = "SELECT 'users.user_email' as 'name' FROM users
inner join patients on 'users.id'='patients.doctor_id'
WHERE 'users.id'='1'";

$result = mysqli_query($query);

while ($row = myslqi_fetch_array($result)) {
    echo $row['name'] ;
}

Like the error says, " mysqli_fetch_assoc() expects exactly 1 parameter, 2 given in "就像错误所说的那样,“ mysqli_fetch_assoc() 只需要 1 个参数,其中 2 个给出

mysqli_fetch_assoc() only expects 1 parameter, but you're passing 2. mysqli_fetch_assoc() 只需要 1 个参数,但你传递了 2 个。

Try this.试试这个。

$query = "SELECT 'users.user_email' as 'name'
FROM users,patients 
WHERE 'users.id'='patients.doctor_id' AND 'users.id'='1';
" ;

$result=mysqli_query($conn,$query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] ;
}

try尝试

 $query = "SELECT 'users.user_email' as 'name' FROM users
    inner join patients on 'users.id'='patients.doctor_id'
    WHERE 'users.id'='1'";

    $result = mysqli_query($query);

    while ($row = myslqi_fetch_array($result)) {
        echo $row['name'] ;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM