/    Sign up×
Community /Pin to ProfileBookmark

string to integer in php

Hi,

My problem:

I have a column whose data type is int.

When my php program is filling the values in the insert statement, the value passed is passed as a string i.e. ’20’ and not 20.

Hence the data is not getting inserted in the table.

How do I pass the value as a integer value and not as a string via php? I want the value to be 20 and not ’20’.

Please can some one help me

Really appreciated the help.

Thanks a lot.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@kromolNov 16.2011 — Use this function intval
Copy linkTweet thisAlerts:
@NogDogNov 16.2011 — My guess (but I'm far from sure without more details or actual source code) is that it's an SQL issue: putting quotes around an integer value in the actual query, e.g.:
[code=php]
$var = 20;
$sql = "INSERT INTO table_name (field_name) VALUES('$var')";
[/code]
...when what you actually would want would be:
[code=php]
$var = 20;
$sql = "INSERT INTO table_name (field_name) VALUES($var)";
[/code]

If those single quotes around $var (in this example) are getting added by some function in your script, then you may want to modify that function to do a is_numeric() check on the variable, and only wrap it in quotes if that function returns false.
×

Success!

Help @meherkbala spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...