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

How to check if a table is empty?

190 100+
Hi Guys,

I want to check if a table is empty or not?

In my table (i.e test) have no records and id is a primary key as well as auto increment column.

i have using to find this "select count(id) from test"

It gives 1 .

ALso using others query but only get answer 1

how do find ?
Aug 25 '08 #1
12 38476
coolsti
310 100+
I think you posted your question in the wrong forum. It seems to be a database issue, not necessarilly a PHP issue. You may wish to post this elsewhere.
Aug 25 '08 #2
Ferris
101 100+
I test it in mysql,it's ok...

Expand|Select|Wrap|Line Numbers
  1. select count(id) from test;
it will return 0
what database do you use?
Aug 25 '08 #3
Atli
5,058 Expert 4TB
Coolist is quite right, of course. This would not belong in the PHP forum, but rather in one of the database forums.
Considering that this was posted in the PHP forum, I have moved it over to the MySQL forum for now. Let me know if it should be somewhere else please.

As to your question, using the COUNT function should give you the results you need.
That is, if you were to do:
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) FROM test;
  2.  
And we assume the test table is empty, it should give you something like:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT COUNT(*) FROM test;
  2. +----------+
  3. | COUNT(*) |
  4. +----------+
  5. |        0 | 
  6. +----------+
  7.  
To get that result in PHP, you would of course have to read the value returned by the query, as opposed to the row count.
Aug 25 '08 #4
maheswaran
190 100+
Hi,

Yes my table is empty. I have using mysql 5.0.51 ....

But have result 1...

What i did wrong ????
Aug 25 '08 #5
Atli
5,058 Expert 4TB
If you did that, and you got 1, then there is 1 row in you table...

Try clearing out the table, like:
Expand|Select|Wrap|Line Numbers
  1. TRUNCATE test;
  2.  
And then try again. See if the result changes.
Aug 25 '08 #6
maheswaran
190 100+
ok

i have another problem now. I thing faukt is in my site.....

I have job table.. and job id is auto increment ... For job entry form i will show the job id in the column job id like below

Job id : 1 (from table job for this am using "select max(jid) from job" )

Job desc: <input box>

Add button

while doing mutiple adding there is no probs ...job id increasing and i show the next job id in form..
but while am did bul delete... There is no record in table.....now job id comes again 1.... not next one.... can any one give some logical idea....
Aug 26 '08 #7
r035198x
13,262 8TB
..
but while am did bul delete... There is no record in table.....now job id comes again 1.... not next one.... can any one give some logical idea....
Could you explain yourself again and post some relevant code?
Aug 26 '08 #8
maheswaran
190 100+
Here My Code

Expand|Select|Wrap|Line Numbers
  1. //Generate Job No
  2. $qry="select max(jid) from jobd";
  3. $res=mysql_query($qry);
  4. $cnt=mysql_num_rows($res);
  5. list($jid)=mysql_fetch_array($res);
  6.  
Expand|Select|Wrap|Line Numbers
  1. <table cellspacing="5" cellpadding="1" border="0" align="center" width="80%">
  2. <td>
  3. <tr><th colspan="2"><br>Job Entry Form</th></tr>
  4. <tr><td>Job No</td><td><input type="text" tabindex="1" name="jobid" value="<? echo $jid;?>" class="normal" readonly></td></tr>
  5. <tr><td>Job Description</td><td><!--input type="text" tabindex="1" name="des" value="<? echo $jdescription;?>"--><textarea name="des"><? echo $jdescription;?></textarea></td></tr>
  6. <tr><td>Client Matter #</td><td><input type="text" tabindex="1" name="cmatter" value="<? echo $cmatter;?>"></td></tr>
  7. <tr><td>Attachment</td><td><input type="file" tabindex="1" id="attachment" name="attachment"></td></tr>
  8. <tr><td></td><td align="left"><input type="hidden" name="pageaction"  value="<?=$action;?>"><input type="submit" name="submit" value="submit">
  9. </table>
  10.  



Here am displayinh job id in entry form and asking user to put related data like description and attachment.....

While every sucessful addi job id increasing . if am added 5 records then am displaying job id 6 in form to be entered ...

Every thing is ok until this...

If am delete all files in table then job id become 1 instead of 6 (before that added 5 records and all reocrds were deleted, now id should come 6.... not one 1)'
Aug 26 '08 #9
r035198x
13,262 8TB
Are you using the value returned from
Expand|Select|Wrap|Line Numbers
  1. $qry="select max(jid) from jobd";
for your next ID? you shouldn't be doing that if you set the column to auto increment. After you delete everything from the table that value will be zero and if you use it's next value then 1 will be saved which is not what you want.
Aug 27 '08 #10
maheswaran
190 100+
k fine.then can you tell me any idea regd this.....to increse my jobid
Sep 1 '08 #11
r035198x
13,262 8TB
k fine.then can you tell me any idea regd this.....to increse my jobid
If you set the columns to auto increment then it will increase automatically.
Sep 4 '08 #12
maheswaran
190 100+
k i did this trick, i have mainting a jobid into another table called temp for every auto increment or deletion there is no effect in this table only in jobid table
Sep 4 '08 #13

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

Similar topics

0
by: Sigurd Urdahl | last post by:
I need to do CHECK TABLE on a lot of tables (actually on all tables in several databases), and hoped to do something like mysql> CHECK TABLE reports.* which ddn't work. The only thing that...
3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
2
by: Paul Telco | last post by:
Hello, I'm a new user designing a simple database to retrieve pre-prepared docunents for printing. I have five tables, a form to design the documents, a form to customise and retrieve the...
30
by: S. van Beek | last post by:
Dear reader A record set can be empty because the condition in the query delivers no records. Is there a VBA code to check the status of a record set, record set empty
1
by: Oleg Ogurok | last post by:
Hi all, I want to use RegularExpressionValidator to enforce non-empty integer format in a TextBox. However, the validator doesn't give the error when the textbox is empty. For example, if...
4
by: whisher | last post by:
Hi. I'm taking my first steps on regex I set up this simple function to check if a form field is empty or with only space. var onlySpaceRegexp = /^\s*$/; function isEmpty(val) { if...
7
by: lphang | last post by:
I am reading a simple text file that will contains three set of string values as follow: "1234567", "ABC123456", "" I have my codes as follow trying to check for an empty string, with the input...
3
by: akshalika | last post by:
I want regular expression which check for empty. pls help me.
18
JustRun
by: JustRun | last post by:
Hi, I just want to know what is the number that will return if the table is empty "has no record" Is it ' 0 ' ?
10
by: klharding | last post by:
I am reading the contents of a text file into variables. All works well if the text file does not exist, or it does exist and contains 4 rows of data. But I need to account for rows being empty or...
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?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.