douzhong1730 2018-06-11 08:55
浏览 110
已采纳

在桌面内的一个字段中列出的MySQL用户名查询另一个表并返回用户结果

So I am new to PHP and I have been on a roll lately developing a website. My question is this. I have all users listed inside of a field in one table.

This is a list of one of these fields: admin, user1, user2, user3.

Now what I need to do is take them users from that ONE field, I am guessing put them into an array and then search each user inside of another table called users and then list the users info such as their rating and bio.

Thanks in advance for any help.

I reposted this with pictures to help you get an idea of what I am looking to accomplish.

I am looking to get the users from this table listed inside of that cell jobs Table

and find them in this table and then list them users table

Hey I have edited the code in the following way and I am getting an error.

$jobID = 1;
$jq = $con-> prepare('SELECT applied_names FROM jobs WHERE jobID=1 LIMIT 1');
$jq-> execute();
$jq-> bind_result($usernames);
$jq-> fetch();
$jq-> close();

$stm = $con->prepare("SELECT username FROM users WHERE username IN (?)");
$stm->execute(array($usernames));
$result = $stm->get_result()->fetch_assoc();
$stm->close();

The error and warning says: Warning: mysqli_stmt::execute() expects exactly 0 parameters, 1 given on line 14

and Fatal error: Call to a member function fetch_assoc() on boolean on line 15

  • 写回答

2条回答 默认 最新

  • duan19780629 2018-06-11 09:01
    关注

    You can use a subquery and the keyword IN for this:

    SELECT * FROM meta_table WHERE userID IN (SELECT id FROM users)

    If you post table & column names, I will be able to give you a more precise query.

    EDIT: after seeing your edited post with the images, this way is not possible. you would still be able to use the IN after you have got the field with the usernames in PHP.

    $usernames = $row['applied_names'];
    $stm = $pdo->prepare("SELECT * FROM users WHERE username IN (?)");
    $stm->execute(array($usernames));
    

    And for mysqli:

    $usernames = $row['applied_names'];
    $stm = $con->prepare("SELECT * FROM users WHERE username IN (?)");
    $stm->bind_param('s', $usernames);
    $stm->execute();
    

    In my opinion, you should have a users_jobs table with columns id userID & jobID then you can use queries like this:

    SELECT username,email,jobName,jobStatus FROM users_jobs 
    JOIN users ON users.id = users_jobs.userID
    JOIN jobs ON jobs.id = users_jobs.jobID
    

    This will produce one row with all the columns you have specified in the SELECT

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)