donglefu6195 2014-11-10 13:57
浏览 49
已采纳

如何在php中搜索从Database中存储为数组的值

How to search an array value, using GET method, to retrieve Data from database?

In other word, I have (prof_ids) Stored in database like this: 1, 15, 52, All those three values are stored in one field. I have a link containing this (?prof_id=1) for example and I want a query to search if the (prof_id) value exists on (prof_ids) field or not. and I have no Idea how to do it.

Is it possible to this?

$sql = $mysqli->query("SELECT * FROM profs WHERE prof_ids LIKE '%".$_GET[prof_id'']."%' ");

I sure my query will not get me the result I'm looking for, if any one please have any other solution can you please help me with this..

  • 写回答

3条回答 默认 最新

  • doucheng2210 2014-11-10 14:03
    关注

    The problem with doing it using LIKE (aside from the SQL injection) is that you will get all sorts of records you might not want. Consider if we pass 1 in. That would match 1, 10, 14, 100, 21, etc

    This is why you need a one-to-many table that contains the data in individual records, so you can match the records exactly. It takes more space but it is the only way to ensure you get the records you want every time.

     SELECT table1.column
     FROM table1
          INNER JOIN table2 ON table1.id = table2.id
     WHERE table2.record = "1"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线