1

I am a little confused about how $query works and how I can find a value... Say I have a checkuser.php script. The purpose here is to echo "Correct" if the user already exists. I have the columns (username, password, email). What I want to know is how can I search the column username for value $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!

Tiskolin
  • 167
  • 17
frrancis909
  • 33
  • 1
  • 4
  • Already answered here: http://stackoverflow.com/questions/9953157/mysql-php-check-if-row-exists Next time google first, I found several results in just a few seconds with this search: https://www.google.com/search?q=php%20check%20if%20row%20already%20exists&rct=j – Matt C May 31 '15 at 17:45

3 Answers3

0

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";};
Tiskolin
  • 167
  • 17
cangak
  • 136
  • 10
0

In your query, you do not need to use the sprintf() function at all. an SQL query is inserted as a regular string.

Furthermore you don't need to use CONCAT() SQL function either.

Then if you want to check against an exact string you can just compare with the = operator instead of using SQL LIKE statement.

SidOfc
  • 4,552
  • 3
  • 27
  • 50
0

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";
Tiskolin
  • 167
  • 17
swifty
  • 181
  • 2
  • 12