douwen1937 2012-05-08 07:12
浏览 105
已采纳

如何使用Recordset中的条目作为MYSQL查询中的条件

I've been experimenting with PHP for the first time so I hope this isn't an obvious question. I have two tables in a database, one is called users the other is called articles. In the users table there is a field which contains the categories that the user wants to follow. Using a recordset in Dreamweaver I can get the information for the current user through a recordset. This done in the code below.

'$row_UserDets['CatFollow']'

At the moment there are comma separated values in the field however I can change this. I now want to search the Article Table's Category Field and return all results with the those categories. So far I've tried this.

$query_usernews = "SELECT * FROM Articles WHERE CategoryOne IN ($row_UserDets['CatFollow']); $usernews = mysql_query($query_usernews, $database name) or die(mysql_error());

I hope that makes sense. I've tried it a few times in a number of different formats. As I've said I'm new to this and I might have been going about it completely the wrong way. man Thanks for your time.

  • 写回答

1条回答 默认 最新

  • dpr26232 2012-05-08 08:32
    关注

    Try something like:

    $query_usernews = "SELECT * FROM Articles WHERE CategoryOne IN ({$row_UserDets['CatFollow']})";
    

    Or simply:

    $query_usernews = "SELECT * FROM Articles WHERE CategoryOne IN (" . $row_UserDets['CatFollow'] . ")";
    

    As Romain stated in the comment, you need to SQL Injection proof your code (mysql_real_escape, or PDO + Prepared statements).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大