RE: If syntax

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

 



Please PMJI, but I'm new to PHP but have a little experience with databases, and I have a question:
 
Is there a particular reason that LIKE is used in the query?  Let's say you had the contrived alias value in the table, 'aaaaa', and one wanted to add the alias, 'aaaa' (or fewer 'a's).  Wouldn't the query misreport that the alias, 'aaaa', already exists?
 
I'm assuming that the number of characters in the alias can vary by some range (e.g., 3 to 10 characters), and that:
  "SELECT alias FROM tablename WHERE alias LIKE '$alias%' LIMIT 1";

becomes:
  "SELECT alias FROM tablename WHERE alias LIKE 'aaaa%' LIMIT 1";

at execution-time.
 
Regards,
Dave

	-----Original Message----- 
	From: Jeffrey [mailto:jeffreyb@xxxxxxxxxxx] 
	Sent: Sun 10/2/2005 3:32 AM 
	To: 
	Cc: PHP DB 
	Subject: Re:  If syntax
	
	

	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 
 
NOTICE: This E-mail may contain confidential information. If you are not
the addressee or the intended recipient please do not read this E-mail
and please immediately delete this e-mail message and any attachments
from your workstation or network mail system. If you are the addressee
or the intended recipient and you save or print a copy of this E-mail,
please place it in an appropriate file, depending on whether
confidential information is contained in the message.

-- 
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