简体   繁体   English

PHP和MySQL搜索栏的价值

[英]PHP & MySQL Search Column for value

I am a little confused about how $query works and how I can find a value... Say I have a checkuser.php script. 我对$query工作原理以及如何找到一个值感到有些困惑...说我有一个checkuser.php脚本。 The purpose here is to echo "Correct" if the user already exists. 目的是在用户已经存在的情况echo “正确”。 I have the columns (username, password, email). 我有列(用户名,密码,电子邮件)。 What I want to know is how can I search the column username for value $username ? 我想知道的是如何在列username搜索值$username This is what I have currently: 这是我目前拥有的:

$query = sprintf("SELECT * FROM users WHERE CONCAT(username) LIKE '$u'");
$result = mysql_query($query);
if(mysql_num_rows($result) != 1)
echo "Username Not Found";

Thanks! 谢谢!

I haven't tested it, but this should work: 我还没有测试过,但这应该可以工作:

$q=mysql_query("select ysername from user where username='$u'");
$count=mysql_num_rows($q);
If ($count>0) { echo "usename fount";};

In your query, you do not need to use the sprintf() function at all. 在查询中,您根本不需要使用sprintf()函数。 an SQL query is inserted as a regular string. SQL查询作为常规字符串插入。

Furthermore you don't need to use CONCAT() SQL function either. 此外,您也不需要使用CONCAT() SQL函数。

Then if you want to check against an exact string you can just compare with the = operator instead of using SQL LIKE statement. 然后,如果要检查确切的字符串,则可以仅使用=运算符进行比较,而不必使用SQL LIKE语句。

This should work. 这应该工作。 However, I didn't test it: 但是,我没有对其进行测试:

$query = "SELECT * FROM users WHERE username = '$u'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
echo "Username Not Found";

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

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