473,401 Members | 2,068 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,401 software developers and data experts.

Finding if a record exists?

Hello

I need to check if a phone number exists in a database. Using the
following SQL statement, what is the right way to code this in PHP?

$dbh = new PDO("sqlite:test.sqlite");
$sql = "SELECT 1 AS number FROM phones WHERE phones_tel='123'";
$row = $dbh->query($sql)->fetch();

//1.
if(!$row['number'])
print "Not found";
else
print "Found";

//2.
if(count($row))
print "Found";
else
print "Not found";

//3.
if(!$row[0])
print "Not found";
else
print "Found";

$dbh = null;

Thank you.
Mar 25 '08 #1
2 4814
Gilles Ganault a écrit :
I need to check if a phone number exists in a database. Using the
following SQL statement, what is the right way to code this in PHP?

$dbh = new PDO("sqlite:test.sqlite");
$sql = "SELECT 1 AS number FROM phones WHERE phones_tel='123'";
$row = $dbh->query($sql)->fetch();
I would say:
$numrows = $dbh->query($sql)->numrows();
I suppose numrows exists as well as fetch, then it should be lighter not
to fetch the data but only ask how many were returned.
Then, $numrows is either a PDO error or an integer which you can check.

Regards,

--
Guillaume
Mar 25 '08 #2
On Tue, 25 Mar 2008 14:14:58 +0100, Guillaume
<gg*****@NOSPAM.gmail.com.INVALIDwrote:
>I would say:
$numrows = $dbh->query($sql)->numrows();
Thanks for the tip, but numrows() doesn't exist in PDO, and roCount()
isn't reliable when used with SELECT:

"For most databases, PDOStatement->rowCount() does not return the
number of rows affected by a SELECT statement. Instead, use
PDO->query() to issue a SELECT COUNT(*) statement with the same
predicates as your intended SELECT statement, then use
PDOStatement->fetchColumn() to retrieve the number of rows that will
be returned. Your application can then perform the correct action."

So I guess the right way is to use "SELECT count(*).

Thanks.
Mar 25 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ST | last post by:
Hi, I'm sort of in a rush here...I'm sort of new to vb.net and I'm trying to write the syntax to check a sql table to see if the record already exists based on firstname and lastname text fields...
1
by: David C. Barber | last post by:
I'm trying to determine if any matching records exist on a LIKE query performing a partial match of last names to a remote back-end database in the most efficient manner possible. LAN Traffic...
5
by: BerkshireGuy | last post by:
Hello everyone, I have a bond form that a user uses to enter data. One of my fields, is PolicyNumber. I added some code on the Before Update event of txtPolicyNumber that checks to see if...
6
by: Opie | last post by:
What would be a more efficient way for me to determine if a record in an SQL DB table exists? Right now, I have a try/catch like this: try {...
3
by: fuimens | last post by:
Hi, With mysql-4.0.20, I have a problem inserting data with foreign key references, MySQL saying ERROR 1216: Cannot add or update a child row: a foreign key constraint fails The message is...
1
by: Pedro Pinto | last post by:
I'm new at access and i'm dealling with a simple problem on positioning the form record seeking a date primary field. I have simple table with two data fields: 1- date field as short-date...
6
by: Matt | last post by:
I need some guidance on how to handle an issue. I have an .asp page that correctly queries a table and returns data if a 'job number' and week ending date exist and the user can update the...
6
by: Helena666 | last post by:
Hi Its been a while since I have built a database using access and vba and am a bit rusty. I am using a command button on a form to write a record to a table, using an append query. However I need...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.