473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rewrite without using try catch

hsriat
1,654 Expert 1GB
IDs in the database are saved in the form of abc_001, abc_002 or they may be abc_asd, abc_a12

I run a code to check the highest numerical suffix of the ids.
in the above case, it would return 3.

Here's the code for that.
Expand|Select|Wrap|Line Numbers
  1. $highest_serial = 0;
  2. while($row = mysql_fetch_array($sql))
  3. {
  4.     $this_serial = str_replace("abc_", "", $row['id']);
  5.     try
  6.     {
  7.         $this_serial = variant_int($this_serial);
  8.     }
  9.     catch(Exception $e) 
  10.     {
  11.         null;
  12.     }
  13.     if (filter_var($this_serial, FILTER_VALIDATE_INT)!=false && $this_serial>$highest_serial)
  14.     $highest_serial = $this_serial;
  15. }
If I don't use try-catch, it gives error for the ids of type abc_asd, and if I don't do variant_int(), it considers abc_002's "002" as a string and thus returns a wrong result.

Do you see any other way to do this without using try-catch...?
May 18 '08 #1
16 1872
TheServant
1,168 Expert 1GB
No idea, but what about using an ID column in the database (numerical auto-increment) and then have an internal ID column with the system you describe. It's just if you are naming something and include numbers in the name, really, it is a string, and so you will always need the variant_int() or something like it to convert it.
May 19 '08 #2
Atli
5,058 Expert 4TB
You can use the is_numeric function to check whether a string is a number or not.

You could try something like:
Expand|Select|Wrap|Line Numbers
  1. function is_id_numeric($id) {
  2.   $pieces = explode("_", $id);
  3.   return is_numeric($pieces[1]);
  4. ]
  5.  
I have to wonder tho, why do you use such complex ID's for you table?
It is faaar simpler to use an integer with auto_increment.
May 19 '08 #3
hsriat
1,654 Expert 1GB
I have to wonder tho, why do you use such complex ID's for you table?
It is faaar simpler to use an integer with auto_increment.
No idea, but what about using an ID column in the database (numerical auto-increment) and then have an internal ID column with the system you describe. It's just if you are naming something and include numbers in the name, really, it is a string, and so you will always need the variant_int() or something like it to convert it.
Actually its a little bit complex than it seems. Ids, can not only be in the form of abc_001, but also xyz_001 or abc_xxx or simply abcdef. And Ids are not made by user, but by an administrator. So if he will enter a prefix say abc and want to generate 10 ids, Ajax will check for the highest prefix with that requested suffix in the database (with I am doing in the given snippet) and pop-up a div with a form showing registration for those 10 ids. So auto increment was no solution. Hope you understand my situation.

Thanks :)
Harpeet
May 19 '08 #4
hsriat
1,654 Expert 1GB
You can use the is_numeric function to check whether a string is a number or not.
Thanks for that is_numeric function. I never knew it.
Done like this...[php]$highest_serial = 0;
while($prefixed_id = mysql_fetch_array($sql))
{
$this_serial = str_replace($prefix."_", "", $prefixed_id['id']);
if (is_numeric($this_serial))
{
$this_serial = variant_int($this_serial);
if ($this_serial>$highest_serial)
$highest_serial = $this_serial;
}
}[/php]

Still have to use variant_int to convert 002 to 2
May 19 '08 #5
TheServant
1,168 Expert 1GB
Actually its a little bit complex than it seems. Ids, can not only be in the form of abc_001, but also xyz_001 or abc_xxx or simply abcdef. And Ids are not made by user, but by an administrator. So if he will enter a prefix say abc and want to generate 10 ids, Ajax will check for the highest prefix with that requested suffix in the database (with I am doing in the given snippet) and pop-up a div with a form showing registration for those 10 ids. So auto increment was no solution. Hope you understand my situation.

Thanks :)
Harpeet
Ahh, that's a toughie. I wondered why you had abc_asd and if it was common. Glad you got that work around though. Are you happy with that solution?
May 19 '08 #6
dlite922
1,584 Expert 1GB
No idea, but what about using an ID column in the database (numerical auto-increment) and then have an internal ID column with the system you describe. It's just if you are naming something and include numbers in the name, really, it is a string, and so you will always need the variant_int() or something like it to convert it.
First good answer you've had. :) lol j/k

I'm with you on this, why is it necessary for you to have letters in your IDs?
May 20 '08 #7
hsriat
1,654 Expert 1GB
Ahh, that's a toughie. I wondered why you had abc_asd and if it was common. Glad you got that work around though. Are you happy with that solution?
Yeh, I wanted to remove try catch, and now it works exactly as expected.

why is it necessary for you to have letters in your IDs?

I explained that. Look at post #4.
These are user ids. Depending upon different department, they can have different prefixes. Like there are 10 persons in HR deptt, their ids will be hr_001 to hr_010. I 11th person joins, he should automatically get id hr_011. And I someone is arrogant enough, he can request for id like hr_arrogant (id with word suffix).

Am I doing right according to you?
May 20 '08 #8
hsriat
1,654 Expert 1GB
I 11th person joins, he should automatically get id hr_011. And I someone is arrogant enough, he can request for id like hr_arrogant (id with word suffix).
I think there's a problem with "F" key of my keyboard.

May 20 '08 #9
dlite922
1,584 Expert 1GB
Yeh, I wanted to remove try catch, and now it works exactly as expected.




I explained that. Look at post #4.
These are user ids. Depending upon different department, they can have different prefixes. Like there are 10 persons in HR deptt, their ids will be hr_001 to hr_010. I 11th person joins, he should automatically get id hr_011. And I someone is arrogant enough, he can request for id like hr_arrogant (id with word suffix).

Am I doing right according to you?
I believe something could be fixed higher up. We're beating a dead horse here.

In a good system tables talk to each other via Primary keys, which are always defined as

id INT NOT NULL AUTO_INCRMENT UNIQUE

(sorry for caps, SQL habit)

You'd have a department table and an employee table. (if its a one to many rel'ship, if its a many to many rel'ship, you'd have a link table in between)

employee Table:
ID
departmentID
number
firstName
lastName
Blah
Blah2

department Table
ID
code
name
blah
etc...


In this example employee.departmentID is a foreign key to deparment.ID

I added employee.number because you could use that for their sequence if you don't want to use ID (for example if you could have two employee's with number 10, but they have different ID's and have different departmentIDs). This is possible through some checking and the responsibility of incrementing the number will be on your shoulders.

I would just use the employee.ID as the number.

To use your funky numbering scheme is to grab the employee number and the deparment code (which is "hr").

Example:

[PHP]

SELECT CONCAT(d.code, "_",e.number) FROM employee AS e, department as d;

[/PHP]

That should display the names as:

hr_0345

In case the number is actually not a number, but a word, this scenario works too. Again, incrementing the employee.number will be your responsibility. You can have

hr_arrogant

Let me know if you have any questions.

-Dan
May 21 '08 #10
Atli
5,058 Expert 4TB
These are user ids. Depending upon different department, they can have different prefixes. Like there are 10 persons in HR deptt, their ids will be hr_001 to hr_010. I 11th person joins, he should automatically get id hr_011. And I someone is arrogant enough, he can request for id like hr_arrogant (id with word suffix).

Am I doing right according to you?
You should seriously consider Dan's suggestion.

The method you described violates one of the basic rules of relational databases, namely the first normal form (1NF).
You should never store more than a single piece of data in a single field.

Check out this article to see more.
May 21 '08 #11
hsriat
1,654 Expert 1GB
@dlite922 and Atli

I agree I'm quite bad at database design. But I think either you are misinterpreting me or I'm in trouble. Though I hope first one is true.

The ids above in the form of hr_123 etc has nothing to do with its identification with the department by the system.

All the ids are in one table. And there's only one table. Department table is not the in picture yet. In fact there's a different field for department, which contains the department code.

When asked for a particular department, query executed would be WHERE `deptt`='hr' but not WHERE `id` LIKE 'hr_%'

Is that where confusion lies?... or am I in trouble?
May 21 '08 #12
Atli
5,058 Expert 4TB
If you have the department name it in the table already, why do you include it in the ID?
(I even think that violates the 2NF as well as the 1NF :P)

I would recommend removing the department prefix from your ID and replacing it either with a simple integer that can simply be auto_incremented or a unique string column.
May 21 '08 #13
hsriat
1,654 Expert 1GB
If you have the department name it in the table already, why do you include it in the ID?
(I even think that violates the 2NF as well as the 1NF :P)

I would recommend removing the department prefix from your ID and replacing it either with a simple integer that can simply be auto_incremented or a unique string column.
Well that are the client's requirements. They want ids of the deptt with a prefix of deptt code.

"Physics deptt should have ids like phy_001, phy_002 ..so on.. while for the temporary candidate, we would like to have ids like phy_x01 , phy_x02 or phy_a, phy_b ... depending upon the wish of department administrator. Administrator should also be enabled to provide special ids like phy_good_student_Atli and phy_good_student_dlite922"


That was what I am told to do.:D
May 21 '08 #14
Atli
5,058 Expert 4TB
Hmm... damn clients always complicating things :P

How about if you create a table structure somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE user (
  2.   UserID VarChar(50) Not Null,
  3.   DepCode VarChar(50) Not Null Default 'phy',
  4.   /* etc... */
  5.   Primary Key(UserID, DepCode)
  6. )
  7.  
Note that the Primary Key consists of both the UserID field and the DepCode field, so each UserID is only unique to the DepCode.

Then you could insert into the UserID field whatever you like, whether it be a number or a string.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO user(UserID, DepCode) VALUES
  2. ('001', 'phy'), ('002', 'phy'), ('good_student', 'phy'),
  3. ('001', 'sci'), ('number_two', 'sci'), ('random_guy', 'sci'),
  4. /* etc */
  5.  
Then, when you select the User info, you could CONCAT those two fields to create the actual key, as requested by your client.
Expand|Select|Wrap|Line Numbers
  1. SELECT CONCAT(DepCode, "_", UserID) AS 'UserKey' FROM user;
  2.  
May 21 '08 #15
hsriat
1,654 Expert 1GB
This sounds impressive.
Will change like suggested.
I never knew Primary Key(UserID, DepCode) could work like this. I though primary key needs to be unique throughout the table.

I'm too bad in db design. Can you refer some tutorials?
I don't even know functions in SQL (like CONCAT). Only things I know are SELECT, INSERT, UPDATE. I recently came to know int(1) is not same as varchar(1) .. :(

And world is too cruel, one needs to be perfect in everything to survive. :D

Regards,
Harpreet
May 21 '08 #16
Atli
5,058 Expert 4TB
I though primary key needs to be unique throughout the table.
And this is very true. A primary key does need to be unique for each row.
But, MySQL can create the key based on up to 16 columns.
Think of it as a hidden column, made up of the values of all the given columns, added one after the other, separated by "_".

So in the table I posted earlier, the primary key for a row with UserID="001" and DepCode="phy" would be "001_phy".
Can you refer some tutorials?
I don't really know any tutorials. I just Google stuff I want to know.
And go through the documentation, of course.
May 21 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: krystoffff | last post by:
Hi all For speed reasons, I would like to rewrite the following query without the subquery : SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2 where field2=X); I thought...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
1
by: DDK | last post by:
How would you go about rewriting a URL such as: http://www.mysite.com/folder/page.aspx?query=WordnotNumber to: http://www.mysite.com/folder/WordnotNumber In other words I would like to put...
1
by: baroque Chou | last post by:
Thanks for the help available on msdn, I have succesful done the rewrite job. But there are 2 problems arise: 1.when I try to rewrite the url from say: www.yoursite.com/beverages.aspx to...
7
by: Stan Canepa | last post by:
I am looking for good documentation to help support rewriting a VB 6 app in Dot Net. I looking for things like VB 6 being unsupported in March 2008, general performance improvements, at what point...
4
by: Fred | last post by:
Is it possible to use throw in javascript without try..catch? As far as I know, you must call it from within a try..catch block, or the function that calls throw must itself be called from within...
5
by: Lupus | last post by:
Hello everybody In apache I used a feature called a "Rewrite rule" inside a .htaccess file to process the URL : i.e. http://www/mypage.html =http://www/page.php?page=mypage Now I'd like to do...
4
by: mainargv | last post by:
hi How do you rewrite codes with " ... va_list va_start va_etc", so that simple c compiler don't have to deal with them. I have written a simple c->verilog compiler but it can't deal with...
3
by: aag | last post by:
Hey there! I've got an URL rewrite issue that I'm wondering if the masters out there can help with :) I have a site that resides at (for example) foo.com - it's a full site with tons of pages...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.