dqiz20794 2012-05-30 17:11
浏览 78
已采纳

登录脚本 - 计算行数或使用COUNT('列')

I've been dabbling abit with a login script (PHP and MySQL), using various online tutorials. Today I became curious as to which way is the best to check if a username and password is valid. Every tutorial I've read so far checks the number of row returned, like this:

SELECT stuff FROM users WHERE username = input_username AND password = input_password
count the rows returned
if rows equal to one
login
else
display error message

I was just thinking if it's equally viable to use COUNT and check that value instead, like this:

SELECT COUNT(stuff) FROM users WHERE username = input_username AND password = input_password
if returned value equal to one
login
else
display error message

I've tried both and both work return the intended result. So is there any reason to choose one over the other?

  • 写回答

2条回答 默认 最新

  • drzyeetvt41077335 2012-05-30 17:15
    关注

    You should definitely go with second SQL-statement if you don't need any of the information stored. COUNT() is better for performance. Although the reason people tend to stick to the first one is because they will be needing information about the user if rows returned is positive.

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题