By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,078 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,078 IT Pros & Developers. It's quick & easy.

how to use "if exists" - INSERT INTO IF EXISTS tMyTable ???

P: n/a
Google can't find me a good example of how to use the "if exists"
syntax in MySql. Is it right that to use it this way:

INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk")
I want to insert into a table but only if the table exists.

How does one, in general, from PHP, test for the existence of a table,
without getting an error message?
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
*** lawrence escribió/wrote (2 Sep 2004 10:05:00 -0700):
Google can't find me a good example of how to use the "if exists"
syntax in MySql. Is it right that to use it this way:

INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk")
As far as I know, it's CREATE TABLE the one that accepts a similar syntax:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options] [select_statement]
"INSERT inserts new rows into an existing table"

http://dev.mysql.com/doc/mysql/en/INSERT.html

How does one, in general, from PHP, test for the existence of a table,
without getting an error message?


You can use mysql_list_tables() and search in the result.
--
--
-+ Álvaro G. Vicario - Burgos, Spain - ICQ 46788716
+- http://www.demogracia.com (la web de humor para mayores de 100 años)
++ «Sonríe, que te vamos a hacer una foto para la esquela»
--
Jul 17 '05 #2

P: n/a
lawrence wrote:
Google can't find me a good example of how to use the "if exists"
syntax in MySql. Is it right that to use it this way:

INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk")

I want to insert into a table but only if the table exists.

How does one, in general, from PHP, test for the existence of a table,
without getting an error message?


I don't know....but in a php script when I put the "@" symbol before a
mysql query, it seems to supress errors...
$blah = @mysql_query("insert something into my table values etc");

Jul 17 '05 #3

P: n/a
In article <41***************@nospamun8nospam.com>, Westcoast Sheri wrote:
lawrence wrote:
Google can't find me a good example of how to use the "if exists"
syntax in MySql. Is it right that to use it this way:

INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk")

I want to insert into a table but only if the table exists.

How does one, in general, from PHP, test for the existence of a table,
without getting an error message?


I don't know....but in a php script when I put the "@" symbol before a
mysql query, it seems to supress errors...
$blah = @mysql_query("insert something into my table values etc");


Offcourse is it much nicer not the get the error in the first place.
And error handling (also hiding warnings) is explained in detail in the
manual ;)

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #4

P: n/a
"Alvaro G. Vicario" <kA*****************@terra.es> wrote in message news:<3q*****************************@40tude.net>. ..
*** lawrence escribió/wrote (2 Sep 2004 10:05:00 -0700):
Google can't find me a good example of how to use the "if exists"
syntax in MySql. Is it right that to use it this way:

INSERT INTO IF EXISTS tMyTable VALUES("xxlk", "lkjlkjlkjljk")


As far as I know, it's CREATE TABLE the one that accepts a similar syntax:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options] [select_statement]


SELECT statements also all the use of EXISTS:

http://dev.mysql.com/doc/mysql/en/EX...ubqueries.html

I wish I knew where to go for definitive answers. The MySql manual
leaves a great deal unanswered. It doesn't say, for instance, that
EXISTS cannot be used with INSERT, so I guess I'll have to experiment.
But if I get a syntax error, I won't know if it is because INSERT
doesn't allow EXISTS, or it does but I wrote it wrong.


How does one, in general, from PHP, test for the existence of a table,
without getting an error message?


You can use mysql_list_tables() and search in the result.


This is a good way to go then. EXISTS is less than useful if I can't
get the info I need about it. Might as well rely on PHP to do all my
testing for me. PHP is much better documented than MySql, anyway,
which makes it easier to experiment with.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.