简体   繁体   English

SQL PHP搜索列名称和值

[英]SQL PHP Search column name with value

Ok so I have a db of Users (ID) and their T or F results to different Hobbies. 好的,所以我有一个用户数据库(ID),并将他们的T或F结果分配给不同的爱好。 So something like this, excuse the formating I didn't know how to space it. 所以像这样,请原谅我不知道如何间隔格式。

ID   | swimming | running | rock climbing | learning to program.

user1       T         F            T                  T

user2       F         T            T                  F 

OK I wanted to do a SQL search where I return all Column names of a table, if the value of that column is "T" where the ID is the users ID. 好的,我想做一个SQL搜索,在其中返回表的所有列名称,如果该列的值为“ T”,则ID是用户ID。

so if the ID is user1, I return swimming, rock climbing, learning to program. 因此,如果ID为user1,我将返回游泳,攀岩,学习编程。

Now I couldn't figure out how to do this with SQL so I figured I could try do this manually with PHP. 现在我不知道如何使用SQL进行操作,因此我想可以尝试使用PHP手动进行操作。

So I tried something like this. 所以我尝试了这样的事情。

 $stmt = $this->con->prepare("SELECT swimming, running, rock, 
 program FROM mytablehere WHERE ID = ?");
 $stmt->bind_param("s", $ID);  // specific ID is went in via here
        $stmt->execute(); 
        $stmt->bind_result( $swimming, $running, $rock, $program);
        $stmt->fetch(); 
        $user = array();
 if($swimming == "T"){
 $swimming ="swimming";
 $user['swimming'] = $swimming; }
  ...
return $user; 

So the idea is it would find the binds that are "T" and insert them into the user array, and leave the ones that are "F" out. 因此,想法是找到“ T”绑定并将其插入用户数组,而将“ F”绑定排除。

However my postman tells me, no data is found. 但是我的邮递员告诉我,没有找到数据。
So my question is how do do I get this sort of function. 所以我的问题是如何获得这种功能。 Is there an SQL search to make this easier or how do I need to approach my PHP code differently. 是否有SQL搜索来简化此操作,或者我需要如何以不同的方式处理我的PHP代码。 Can I not if then the bind $swimming or do I have to bind them all to one array, and then return a different array at the end? 如果不是,那么可以绑定$ swimming吗?还是必须将它们全部绑定到一个数组,然后最后返回另一个数组?

if($user['swimming'] == "T"){
    $hobby['swimming'] = "swimming"; }

  return $hobby

there should really be an easier way to do this. 确实应该有一个更简单的方法来做到这一点。 If anyone has an sql search that would be great. 如果任何人都有一个sql搜索,那就太好了。 But this will allow you to do it via php. 但是,这将允许您通过php进行操作。 I was having issues with my postman which was stopping me from getting data, but once I did the code up top will just give you an useless mess. 我的邮递员遇到问题,这使我无法获取数据,但是一旦我完成了代码编写,这只会给您带来无用的麻烦。 This will return only the column names but it has to be done manually. 这将仅返回列名称,但必须手动完成。

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

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