473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Check a record exists

nathj
938 Expert 512MB
Hi,

I'm fairly new to MySQL and learning as I go, generally speaking so far so good.

At present I have a question to which I cannot find an answer, so I was hoping someone may be able to help me out.

For the site I am writing the user my apply to become a member (this is for a leadership forum) in doing so they have to supply a user name. I want to check the database to see if the user name is available and if it is great but if it is not then I need to tell the applicant.

I was hoping to do this without returning a bunch of data, for security reasons. I would like to be able to simply seek for the record based on the supplied user name and return true if it's there and false if it's not.

What is the syntax for this? I used to write Visual FoxPro databases and it was simply
llRecordThere = seek(username, tablename, indexname)

and that did the trick. Is there something similar in MySQL?

Many thanks
Nathan
Jun 26 '07 #1
8 12817
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. SELECT 1 FROM `tablename` WHERE `username` LIKE 'name'
Jun 26 '07 #2
nathj
938 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. SELECT 1 FROM `tablename` WHERE `username` LIKE 'name'
Ok I have played around with this, am I correct in thinking that this simply returns one column, and the number of rows indicates how many times the search expression was found?

This will really help me out. i better go see if there's someone out there I can help.

Many thanks
Nathan
Jun 26 '07 #3
Hey nathj

Try A Stored Procedure That Returns 0/1.

IF EXISTS (SELECT 1 FROM `tablename` WHERE `username` = 'name')
RETURN 1
ELSE
RETURN 0

I Don't See The Need To Use LIKE 'Username'. A Straight Equivalence Check Is More Appropriate
Jun 28 '07 #4
nathj
938 Expert 512MB
Hey nathj

I Don't See The Need To Use LIKE 'Username'. A Straight Equivalence Check Is More Appropriate
I have now used a straight equivalence check, it does make sense. What I have got now is:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) as test FROM table where condition
  2.  
This means that in the PHP I can check to see if test is greater than 0, if it the value in question is already in use.

I was playing around with all this yesterday (changed to straight equivalence this morning) and all is now working fine.

I must admit I am intrigued by the idea of stored procedure, how would this interact with PHP and JavaScript? Them aim of this check is to verify if a username has already been selected as a user fills in the membership application form.

Cheers
nathj
Jun 29 '07 #5
code green
1,726 Expert 1GB
Are you looking for the most efficient query to check if a record exists in a table.Your function call hides a lot of overhead so may not be as efficient as you think.
Expand|Select|Wrap|Line Numbers
  1. llRecordThere = seek(username, tablename, indexname)
I am not sure if COUNT() is better than SELECT 1 because COUNT is a function call. I read somewhere that SELECT 1 was the fastest.
Jun 29 '07 #6
nathj
938 Expert 512MB
Are you looking for the most efficient query to check if a record exists in a table.Your function call hides a lot of overhead so may not be as efficient as you think.
Expand|Select|Wrap|Line Numbers
  1. llRecordThere = seek(username, tablename, indexname)
I am not sure if COUNT() is better than SELECT 1 because COUNT is a function call. I read somewhere that SELECT 1 was the fastest.
That seek line looks exactly like what I had in mind. How do I then submit that from my php page to the database?

Many thanks
Nathan
Jun 29 '07 #7
code green
1,726 Expert 1GB
That seek line looks exactly like what I had in mind.
That seek line is your seek line. I quoted it to explain there is a lot more code behind the scenes than seen in this one line.
How do I then submit that from my php page to the database
What you need to do is create a MySql query as a string, then submit the query using the php function mysql_query().
I suggest you learn the PHP MySql basics first.
Jun 29 '07 #8
nathj
938 Expert 512MB
That seek line is your seek line. I quoted it to explain there is a lot more code behind the scenes than seen in this one line. What you need to do is create a MySql query as a string, then submit the query using the php function mysql_query().
I suggest you learn the PHP MySql basics first.
I didn't realise you were quoting my desired result line in your post. That was not a UDF in the language I mentioned it was built in data manipulation that was fully optimized and a very effecient way to check if a record existed in a table without having to return a full data set. I was also not asking for a way to write a user defined function I was presenting it as an eample of what I was ultimatley after. I am sorry if that cased you confusion in helping me.

I have got code working just fine now to do exactly what I want using PHP and MySQL taking advantage of a data abstraction layer to fully modularise the code. Now my form will validate against format rule and database contents as the user completes it, in this way they can only proceed once each page of the form is valid.

Thanks for your help on this.
nathj
Jun 29 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
5
by: Notgiven | last post by:
You want to check scheduling conflicts and you have a record like: appointments(table): apptID beginningDate endingDate beginningTime endingTime It's easy enough to check if a time is...
3
by: byeung | last post by:
Hi, I am trying to check if a particular record already exists in an Access database through Excel vba code. Through code obtained at another forum, I got the following: ...
10
by: sara | last post by:
Hi - I have been struggling with solution ideas for this now for almost 2 weeks, and have not been able to figure this out. I have a user who creates a Purchase Order (tblPOData). In some...
9
reginaldmerritt
by: reginaldmerritt | last post by:
I have a table which uses two primary keys. One is a canidate code the other is a course\programme code. I have a fom with these two fields but automaticaly picks the candidates code. Therefore...
10
by: bluray | last post by:
Hello guys, thanks again for taking the time to help me out with my problems! This problem seems super simple in my head, however getting the coding to make it work is turing out to be not so...
5
by: vivekgoyal | last post by:
hello i have some problem.. actually i make a userscreen using php and problem is that i have check the username is already exists aur not... i used the query like this... $con =...
2
by: thatguyNYC | last post by:
Hello-- I have a form that lets the user edit the values in a lookup table. The form is bound to a table and has one textbox on it. The record nav buttons are enabled. On the top half of the...
1
by: gurmet | last post by:
Hi All I have been looking around for help, and finally post this problem. I created a form to edit a record. Before i can click save button on the edit form i need to check if the data that...
7
by: djpaul | last post by:
Hello! I want to check if a record exists in the last 30 records that were added. For example, i have a table wich contains about 34000 records by now. But i want to check if the songID is in the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.