473,385 Members | 1,536 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,385 software developers and data experts.

Type conversion

nathj
938 Expert 512MB
Hi,

I'm back with another question. This time I am conviced the answer is simple, I just can't find it anywhere.

I have a variable that comes from a drop down on a form. The variable may be somethig like "14", however, I need to write this to an integer field in a MySQL database.

So, how do I convert "14" to 14 so that I can use it in the database?

Cheers
nathj
Jul 24 '07 #1
4 1371
mwasif
802 Expert 512MB
You can use str_replace() to replace " from the input or the following RegEx to remove everything except numbers.

[PHP]echo preg_replace('/[^0-9]/', "", $_POST["value"]);[/PHP]
Jul 24 '07 #2
nathj
938 Expert 512MB
You can use str_replace() to replace " from the input or the following RegEx to remove everything except numbers.

[PHP]echo preg_replace('/[^0-9]/', "", $_POST["value"]);[/PHP]
Sorry it's taken me so long to get back to this one. I'm in the process of trying it out, if I come unstuck I'll post back with some code.

Cheers
nathj
Jul 27 '07 #3
kovik
1,044 Expert 1GB
PHP automatically converts numerical strings to numbers when used in mathematical operations and conditional statements, and MySQL does the same when assigning a string to a numerical field. Unless the quotes are actally in the string, you should be fine. If they are in the string, you may wish to rethink the values you are giving your to you dropdown options, as it is strange to put double quotes into the actual value.
Jul 27 '07 #4
nathj
938 Expert 512MB
PHP automatically converts numerical strings to numbers when used in mathematical operations and conditional statements, and MySQL does the same when assigning a string to a numerical field. Unless the quotes are actally in the string, you should be fine. If they are in the string, you may wish to rethink the values you are giving your to you dropdown options, as it is strange to put double quotes into the actual value.
volectricicty,

Of course! I don't know what I was thinking when I wrote that bit of code. I have sorted out the code that builds the drop down so that the value is not encased in quotes and all is well without any need for parsing the value.

On the plus side I have learnt something new, and have improved some more of my code. A good result all round.

Many thanks to all who posted.
nathj
Jul 27 '07 #5

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

Similar topics

4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
by: Martin v. Lwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.