Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if table exists?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
how do i check if a table exists in a if statement?
 
You don't have to check, because tables don't exist in if statements. Tables exist in databases.

Insufficient data for a meaningful answer. What's your database backend? ______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
Once you have made your database connection, use this: $table_list = mysql_list_tables($connection);

while ($row = mysql_fetch_row($table_list)) {
echo &quot;$row[0]<br>&quot;;
} ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
That's not working. If I have helped you just click the first link below to let me know :)
 
ristmo2001,
How can it be &quot;not working&quot;, its the most effective way of finding what tables truly exist - if the one you are looking for isnt in the list, your pretty safe to assume it doesn't exist or you've got the wrong database.

I'm now a little unsure exactly how you want to check for a table with an if statement if you don't connect and check what tables exist in the first place. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR, it doesn't work on my script either.

i wanted to check if a table exists in an if statement because i am implementing a install function into my script. so i need the if statement to check if the tables exist and if not i want it to make them. how would i go about doing that? thanks.
 
mysql_list_tables takes a databasename as argument, not a connection pointer. So the syntax would be:
$table_list = mysql_list_tables(&quot;dbname&quot;);
$tables = array();
while (list($tablename) = mysql_fetch_row($table_list))
{
$tables[$tablename] = 1;
}
Then to check if a table exists, just do
if ($tables['tablename'] == 1)
{
// Table exists.
}
else
{
// Table doesnt exist.
} //Daniel
 
Odd about it not working, I've never had a problem with it...still I digress.

CREATE TABLE IF NOT EXISTS table_name etc etc

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR, using the &quot;IF NOT EXISTS&quot; clause is a good idea. Unless you need to know whether the table existed before the SQL statement ran. Whether it actually creates the table or not, the statement runs correctly, so no error is returned to PHP.
______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
It seemed like the best solution for an installer which should always at least try to succeed.

Obviously you'd need rigorous error checking on the connection and database selection/creation before running the table create tho :)

Daniel, you are absolutely right, I guess I should pay more attention when I post late at night ;-) - if everyone was so observant they'd figure these things out for themselves and wouldn't need us at all [lol] ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top