Re: If syntax

From: Date: Sun, 02 Oct 2005 10:32:54 +0000
Subject: Re: If syntax
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-40444@lists.php.net to get a copy of this message
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

« previous php.db (#40444) next »