duanji9378 2012-09-07 04:10
浏览 138
已采纳

PHP sql语句搜索数据库中的用户名

I am trying to construct an sql query to search my database for users. It is supposed to help users to find other users on my website to find friends.

I did some research because I knew I did not want:

$query = "SELECT userName, userID FROM user WHERE userID = $userName ";

Because it would not be an effective search if they had to type the users exact name in to find them.

After doing some research I decided to try the like term with % symbols in front and back so it could be the name with lets say numbers before or after and it would show up.

The example I found online formatted it like this:

$query = "SELECT userName, userID FROM user WHERE userID Like '%{$userName}%' ";

but when I executed this query while implemented on m y website, I ran into problems of it returning to many results. It returned results that did not include anything within my search term.

I also tried the above search query with out the brackets since I did not understand why I needed them but I got the same results.

Any suggestions on how to make the search a little stricter using the above command query?

Or any other suggestions for how I should search my database for a user to friend?

  • 写回答

3条回答 默认 最新

  • duanli6834 2012-09-07 04:17
    关注

    The % sign is for wildcards. So if $userName = 'apple' your query would match apple, applepie, and crabapple but not appl.

    If you want an exact match remove the %s or the LIKE altogether.

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

报告相同问题?

悬赏问题

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