You are viewing this forum as a guest. Login to an existing account, or create a new account, to reply to topics and to create new topics.
I have the following statement:
$table = 'customer_lookup'; if (mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'"))) { print 'It Already Exists!'; } else { mysql_query($sql) or die(mysql_error()); }
...and I was wondering if there was a tidier way of writing the statement, - something more along the lines of:
$table = 'custom_lookup'; if (mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'"))) { exit / die / just stop there and don't give me another table!!; }
There is an and , but there is no clear documentation on just what exactly the first does and the documentation on the latter is so horrendously obfuscated that just what that does is anyone's guess; although on the odd occasion when I have tried to use it it has invariably blanked out the entire site.
I realise, too, that I may be able to make the statement a not statement, but I would like to try and get the other options working if possible and not have to rely on (often awkward workarounds) using such statements.
Last edited by Design_Wholesale (01-28-2011 18:34:46)
Offline
If this is being called by something else use return to go back to the caller. If you want to stop everything use exit. There isn't enough context to know/understand what you're actually trying to do.
Offline
Well I still cannot make out the purpose of or , - they both seem to be as pointless as ( works, so why do we need a seperate function as an AKA for it), and I am sticking with
$table = 'customer_lookup'; if (mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'"))) { print ''; } else { mysql_query($sql) or die(mysql_error()); }
I would have thought it would be obvious why I needed what I was looking for, though. - Basically I don't want a customer_lookup table being generated every time the script was run, so I was hoping there would be some nice clean method of telling the script to ignore the table creation process if the table already exists.
Last edited by Design_Wholesale (01-30-2011 13:24:42)
Offline
endif is simply an alternate way of ending an if statement and precludes the use of braces. return is used to give control back to a caller versus exit or die which terminates execution of everything.
I would modify your test slightly to make it clearer what it is doing and eliminate the other condition which you don't really need/use (table and column names should be surrounded with back ticks not single quotes).
if (mysql_num_rows( mysql_query("SHOW TABLES LIKE `$table`")) == 0) { mysql_query($sql) or die(mysql_error()); }
]
Offline
Thank-you, that is helpful, as it the information on back ticks, - I had been wondering (reading up, too) on when I should be using those and when not.
Also, I found an interesting article on that , too:
http://www.phpfreaks.com/blog/or-die-must-die
...which, if implemented, would change the above to:
if (mysql_num_rows( mysql_query("SHOW TABLES LIKE `$table`")) == 0) { mysql_query($sql) or die('Query failed: ' . mysql_error($db)); }
being dependent on the actual variable used in the code to reference the database, of course.
Offline
For production code simply dieing like that is likely to expose information about your table(s) and/or column(s) which is generally not a good idea. You should of course check for an error but provide some sort of generic error page while logging the actual error to a file that you may examine later.
Offline
Fair enough, - thank-you - something else for me to look at in more depth. - I am actually in the process of bringing all these notes together on our website and expanding on the PHP / MySQL notes, so I will do some experimentation on this and post later.
Offline