Re: If syntax

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



See below...

Ron Piggott wrote:

Take a look at my if ( ) syntax line below.  I am wondering if I have it
wrong in same way.  What I am trying to achieve is if the alias the user
enters is already found in the database I want the words "Alias already in
use" to be shown on the screen; otherwise "Unique alias" to be shown on the
screen.

PRESENTLY alias already in use is what comes up all the time.

Ron


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tablename WHERE alias LIKE '$alias%'";

if (mysql_query($query))

{

echo "Alias already in use";

} else {

echo "Unique alias";

mysql_close();

}

Whether or not there are any matches, you will always have a database query - hence mysql_query($query) will always produce something

Instead you need to see if there are any  matches to your variable.

Also, "SELECT * FROM [some_table] is tremendously inefficient when you only need to count results. You are basically calling up every result in your entire table just to see if a single column entry matches a variable.

I would suggest something like..

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT alias FROM tablename WHERE alias LIKE '$alias%' LIMIT 1";

$result=(mysql_query($query))

if (mysql_num_rows($result)==1)

{

echo "Alias already in use";

} else {

echo "Unique alias";

mysql_close();

}
Good luck,

Jeffrey

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux