Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:42:47+00:00 2026-05-27T15:42:47+00:00

I try to make function to check if table exists and to get total

  • 0

I try to make function to check if table exists and to get total number of rows but I get result 1 instead of 99999.
So, what should I do here to work properly?

int sql_table_length(char* database, char* dtable, char* mysql_user_name, char* mysql_password)
{
    int retval = 0;
    MYSQL *conn;
    conn = mysql_init(NULL);
    if (conn)
    {
        if (mysql_real_connect(conn, "localhost", mysql_user_name, mysql_password, database, 0, NULL, 0)!=0)
        {
            char chktable[512] = {0};
            sprintf(chktable,"%s%s%s", "SHOW TABLES LIKE '", dtable, "'");
            mysql_query(conn, chktable);
            if (mysql_store_result(conn))
            {
                MYSQL_RES *result;
                char lngtable[512] = {0};
                sprintf(lngtable, "%s%s", "SELECT COUNT(*) FROM ", dtable);
                if (!(mysql_query(conn, lngtable)))
                {
                    result = mysql_store_result(conn);
                    retval = mysql_num_rows(result); // here I get 1
                    mysql_free_result(result);
                }
                else retval = -4;   //no rows
            }
            else retval = -3;       //don't exist
        }
        else retval = -2;           //can't connect
    }
    else retval = -1;               //no connection
    mysql_close(conn);
    return retval;
}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:42:48+00:00Added an answer on May 27, 2026 at 3:42 pm

    Your query, SELECT COUNT(*) FROM table returns a single row containing the number of rows in the table. Instead of counting the number of rows in the result, you want to query the number that is returned.

    You will want to do something like (untested):

    MYSQL_STMT *stmt;
    MYSQL_BIND bind;
    long rows;
    my_bool error; /* Omit at your own risk */
    
    stmt = mysql_stmt_init(mysql)
    mysql_stmt_prepare(stmt, lngtable, strlen(lngtable));
    mysql_stmt_execute(stmt);
    
    memset(&bind, 0, sizeof(bind));
    bind.buffer_type = MYSQL_TYPE_LONG;
    bind.buffer = (char *)&rows;
    bind.error = &error; /* Omit at your own risk */
    
    mysql_stmt_bind_result(stmt, &bind);
    
    mysql_stmt_fetch(stmt);
    printf("Number of rows: %ld\n", rows);
    

    You should add error checking to all the mysql calls. See http://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-fetch.html for more examples about binding parameters.

    Alternatively, you can change your query to something like SELECT * FROM table and keep the rest of your code, but this is asking MySQL to do a lot more work, and the result will likely take longer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to make a registration form. When user post the fields, i check
I will try to make this as clear as I can, but if you
I'm fairly new to C# but I will try to make this quick! ;)
I get the following error in the UDK Frontend when I try to make
I try to load some js files dynamically,for example: function openInforWindow(){ //check if the
I'll try to make this as straight forward as possible. Currently our team has
When I try to make a very large boolean array using Java, such as:
I'm looking to try to make an efficient delete method to work on a
I am starting this question to try and make a central point developers can
What will be the code when I try to make a combo box read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.