sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
maheswaran's Avatar

How to check if a table is empty?


Question posted by: maheswaran (Member) on August 25th, 2008 06:35 AM
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 ?
12 Answers Posted
coolsti's Avatar
coolsti August 25th, 2008 08:33 AM
Needs Regular Fix - 304 Posts
#2: Re: How to check if a table is empty?

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.
Ferris's Avatar
Ferris August 25th, 2008 09:33 AM
Member - 102 Posts
#3: Re: How to check if a table is empty?

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?
Atli's Avatar
Atli August 25th, 2008 09:37 AM
Moderator - 2,759 Posts
#4: Re: How to check if a table is empty?

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;

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

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.
maheswaran's Avatar
maheswaran August 25th, 2008 12:27 PM
Member - 113 Posts
#5: Re: How to check if a table is empty?

Hi,

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

But have result 1...

What i did wrong ????
Atli's Avatar
Atli August 25th, 2008 01:31 PM
Moderator - 2,759 Posts
#6: Re: How to check if a table is empty?

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;

And then try again. See if the result changes.
maheswaran's Avatar
maheswaran August 26th, 2008 01:22 PM
Member - 113 Posts
#7: Re: How to check if a table is empty?

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....
r035198x's Avatar
r035198x August 26th, 2008 01:50 PM
Administrator - 11,298 Posts
#8: Re: How to check if a table is empty?

Quote:
Originally Posted by maheswaran
..
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?
maheswaran's Avatar
maheswaran August 26th, 2008 02:08 PM
Member - 113 Posts
#9: Re: How to check if a table is empty?

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

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>





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)'
r035198x's Avatar
r035198x August 27th, 2008 07:45 AM
Administrator - 11,298 Posts
#10: Re: How to check if a table is empty?

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.
maheswaran's Avatar
maheswaran September 1st, 2008 11:23 AM
Member - 113 Posts
#11: Re: How to check if a table is empty?

k fine.then can you tell me any idea regd this.....to increse my jobid
r035198x's Avatar
r035198x September 4th, 2008 07:25 AM
Administrator - 11,298 Posts
#12: Re: How to check if a table is empty?

Quote:
Originally Posted by maheswaran
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.
maheswaran's Avatar
maheswaran September 4th, 2008 12:40 PM
Member - 113 Posts
#13: Re: How to check if a table is empty?

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
Reply
Not the answer you were looking for? Post your question . . .
196,991 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,991 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top MySQL Contributors