How to check if a table is empty?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheswaran
    New Member
    • Mar 2007
    • 190

    How to check if a table is empty?

    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 ?
    Last edited by Atli; Aug 25 '08, 08:38 AM. Reason: Moved from the PHP forum.
  • coolsti
    Contributor
    • Mar 2008
    • 310

    #2
    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.

    Comment

    • Ferris
      New Member
      • Oct 2007
      • 101

      #3
      I test it in mysql,it's ok...

      Code:
      select count(id) from test;
      it will return 0
      what database do you use?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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:
        [code=mysql]
        SELECT COUNT(*) FROM test;
        [/code]
        And we assume the test table is empty, it should give you something like:
        Code:
        mysql> SELECT COUNT(*) FROM test;
        +----------+
        | COUNT(*) |
        +----------+
        |        0 | 
        +----------+
        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.

        Comment

        • maheswaran
          New Member
          • Mar 2007
          • 190

          #5
          Hi,

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

          But have result 1...

          What i did wrong ????

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            If you did that, and you got 1, then there is 1 row in you table...

            Try clearing out the table, like:
            [code=mysql]
            TRUNCATE test;
            [/code]
            And then try again. See if the result changes.

            Comment

            • maheswaran
              New Member
              • Mar 2007
              • 190

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

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                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?

                Comment

                • maheswaran
                  New Member
                  • Mar 2007
                  • 190

                  #9
                  Here My Code

                  [code=php]
                  //Generate Job No
                  $qry="select max(jid) from jobd";
                  $res=mysql_quer y($qry);
                  $cnt=mysql_num_ rows($res);
                  list($jid)=mysq l_fetch_array($ res);
                  [/code]
                  [code=html]
                  <table cellspacing="5" cellpadding="1" border="0" align="center" width="80%">
                  <td>
                  <tr><th colspan="2"><br >Job Entry Form</th></tr>
                  <tr><td>Job No</td><td><input type="text" tabindex="1" name="jobid" value="<? echo $jid;?>" class="normal" readonly></td></tr>
                  <tr><td>Job Description</td><td><!--input type="text" tabindex="1" name="des" value="<? echo $jdescription;? >"--><textarea name="des"><? echo $jdescription;? ></textarea></td></tr>
                  <tr><td>Clien t Matter #</td><td><input type="text" tabindex="1" name="cmatter" value="<? echo $cmatter;?>"></td></tr>
                  <tr><td>Attachm ent</td><td><input type="file" tabindex="1" id="attachment " name="attachmen t"></td></tr>
                  <tr><td></td><td align="left"><i nput type="hidden" name="pageactio n" value="<?=$acti on;?>"><input type="submit" name="submit" value="submit">
                  </table>
                  [/code]




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

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Are you using the value returned from
                    Code:
                    $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.

                    Comment

                    • maheswaran
                      New Member
                      • Mar 2007
                      • 190

                      #11
                      k fine.then can you tell me any idea regd this.....to increse my jobid

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        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.

                        Comment

                        • maheswaran
                          New Member
                          • Mar 2007
                          • 190

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

                          Comment

                          Working...