Kryptronic Software Support Forum

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.

#1 01-28-2011 18:24:11

Design_Wholesale
Member
From: England!
Registered: 11-21-2008
Posts: 1104
Website

Is There A Tidier Way To Make This if Statement Exit?

I have the following statement:

Code:

$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:


Code:

$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

 

#2 01-29-2011 04:12:27

Dave
Member
Registered: 07-05-2003
Posts: 11233

Re: Is There A Tidier Way To Make This if Statement Exit?

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

 

#3 01-30-2011 13:23:30

Design_Wholesale
Member
From: England!
Registered: 11-21-2008
Posts: 1104
Website

Re: Is There A Tidier Way To Make This if Statement Exit?

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

Code:

$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

 

#4 01-30-2011 14:11:04

Dave
Member
Registered: 07-05-2003
Posts: 11233

Re: Is There A Tidier Way To Make This if Statement Exit?

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).

Code:

if (mysql_num_rows( mysql_query("SHOW TABLES LIKE `$table`")) == 0) {

mysql_query($sql) or die(mysql_error());

}

]

Offline

 

#5 01-30-2011 14:22:01

Design_Wholesale
Member
From: England!
Registered: 11-21-2008
Posts: 1104
Website

Re: Is There A Tidier Way To Make This if Statement Exit?

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:


Code:

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

 

#6 01-30-2011 16:00:45

Dave
Member
Registered: 07-05-2003
Posts: 11233

Re: Is There A Tidier Way To Make This if Statement Exit?

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

 

#7 02-06-2011 19:04:50

Design_Wholesale
Member
From: England!
Registered: 11-21-2008
Posts: 1104
Website

Re: Is There A Tidier Way To Make This if Statement Exit?

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

 

Board footer