Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Function to check if table exists?


ArtcoInc

Recommended Posts

Is there currently a function in osC to check to see if a table exists in the database?

 

I have found a number of code examples on the web, but don't want to have to re-invent the wheel if I don't have to ;)

 

Thanks!

 

Malcolm

Link to comment
Share on other sites

@@wHiTeHaT

 

While I don't understand it, this works fine. Thanks!

 

 

I created a new, empty table called 'test'. Then, using this code:

<?php
  $CheckTable = tep_db_query("SHOW TABLES LIKE 'test'");
  if( tep_db_num_rows($CheckTable) > 0 ) {
    echo 'Table exists <br />';
    echo tep_db_num_rows($CheckTable) . '<br />';
  } else {
    echo 'Table does not exist <br />';
  }
?>

it returns:

Table exists
1

yet, phpMyAdmin shows that there are zero rows. I'm confused ... :wacko:

 

Malcolm

Link to comment
Share on other sites

tep_db_num_rows() returns the number of rows in the query, not the number of rows in the table. Since there is one table named test, the query returns one row representing that one table.

 

If you want to know the number of rows in the table, you'll need a new query like:

$test = tep_db_query("select * from 'test'");
echo tep_db_num_rows($test) . '<br />';

Regards

Jim

See my profile for a list of my addons and ways to get support.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...