How to know if user input it string or integers?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

How to know if user input it string or integers?

Post by scheinarts »

Hi,

I need to perform a different mysql query depending of the input of the user (string or numbers)

How do i properly code my if(statement) for this ?

The other solution would be to code query to search in two different fields but i do not know how to do this. I think option 1 is the most sane.

Thank you in advance for any help!

This is my first post by the way :D
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Code: Select all

<?php
if(is_numeric('$your_input')){
// this input contains only numers
} else {
// string contains not only numbers
}
?>
I would recomend for you to also check with preg_match elseif - if string contains ONLY letters.
Here is example that takes only letters from string:

Code: Select all

$incomin_variable="test2";
preg_match ("/([a-zA-Z-]+)/",$incomin_variable,$outcomin_variable);
print_r($outcomin_variable);
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Taking only letters from a string does not mean that the input is numeric when there are no letters, for example #123 is string but it does not contain any letter.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: How to know if user input it string or integers?

Post by stereofrog »

scheinarts wrote:
I need to perform a different mysql query depending of the input of the user (string or numbers)
You need to define more precisely what "string" and "number" are in this case. For example, is "-.3434e3" a "number"?
The other solution would be to code query to search in two different fields but i do not know how to do this. I think option 1 is the most sane.
You might want to tell us more about what you're after.

This is my first post by the way :D
Welcome to the forums, scheinarts! :)
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post by scheinarts »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks for your replies guys!!!   

OK, what I mean by string is literally a jpg filename (pic_name_123.jpg) <-something like thi every time

or 

they can use strickly a number (123 or 687 or 984 etc etc) the id number

Could be ONLY these two scenarios

Now, to search for numbers (id number), it has to be in a specific field inside the database table and to search for a string (filename) has to be in a different specific field in the table

So my idea is to perform a different query depending on the input

Here is the actual mysql query...

For strings (filename) case:
[syntax="sql"]select p.products_id, p.products_model, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and  [b]p.products_image[/b] like '%" . tep_db_input($search) . "%' order by p.products_model
For numbers (id number) case:

Code: Select all

select p.products_id, p.products_model, pd.products_name, p.products_quantity, p.products_image, p.products_price, p.products_date_added, p.products_last_modified, p.products_date_available, p.products_status, p2c.categories_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and p.products_id = p2c.products_id and  [b]p.products_model[/b] like '%" . tep_db_input($search) . "%' order by p.products_model

In bold are the differece in the two queries, so unless there is a way to merge them together, i guess the if else idea for numbers or strings should work

So which is the proper way to handle this issue?

Thanks for your quick replies. I like these forums!!!



feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post by scheinarts »

thanks :)
Post Reply