PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 20th, 2002, 04:26 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
PHP has a variable as string, and MySQL expects it as interger!

Inside a PHP-script I have constructed an INSERT sql_statement with string vars [formated with format_number()], but when executing the query it dies. I think it's because some vars are expected as intergers by MySQL. How can I convert those vars to execute the query successfully?
Thank you for your help,
Freddy

Reply With Quote
  #2  
Old August 20th, 2002, 04:38 PM
bakertrg's Avatar
bakertrg bakertrg is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Scottsdale AZ, US
Posts: 2,253 bakertrg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 45 sec
Reputation Power: 4
Send a message via Yahoo to bakertrg
RE: PHP has a variable as string, and MySQL expects it as interger!

what is the data in the vars? If you don't explicitly force a data type PHP will change type to suit the needs of the operation. You could always edit the db to make the field types text.

B

Reply With Quote
  #3  
Old August 20th, 2002, 04:46 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
RE: PHP has a variable as string, and MySQL expects it as interger!

Is changing the data type in the database the only solution?
There is a posibility that the MySQL database(tables) is going to be used by other applications in another plataform.
*-
When I query the data the first time around some are intergers and floats, but when I show them into a form they are converted automatically to strings, and that's no good with MySQL to inserting or modifing queries.

The NUMBER_FORMAT() build-in function automatically converts the vars to strings!

*-
I appreciate your help very much. Have a nice day,
Freddy

Reply With Quote
  #4  
Old August 21st, 2002, 02:37 AM
siteworkspro.com siteworkspro.com is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 92 siteworkspro.com User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to siteworkspro.com Send a message via AIM to siteworkspro.com
RE: PHP has a variable as string, and MySQL expects it as interger!

Could you please post your SQL query, so that we can take a look

Reply With Quote
  #5  
Old August 21st, 2002, 05:11 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: PHP has a variable as string, and MySQL expects it as interger!

hmm ...

the db field is type int/float, you number_format() the field for presentation, your allowing a user to modify the altered field and then inserting the user-modified number back into the db?

I pull int's from my db and display them in a textbox for the user to change and write the number back to the db with no problem. One reason why I love PHP, don't have to worry about casting. I'm sure php does have a cast() that you can use ... I just checked the php manual and was not able to find a cast().

Anybody know how to cast in PHP?

Reply With Quote
  #6  
Old August 21st, 2002, 06:49 AM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: PHP has a variable as string, and MySQL expects it as interger!

Quote:
but when executing the query it dies. I think it's because some vars are expected as intergers by MySQL.


no, this is not a reason why query dies... mysql does his own type casting if u used ' to surround fields like:

insert into table set field1='data1', field2='data2'...

and if u pass a string in a field that is a int, it would convert it to 0, but it wont die...

and if u don't use ' around field data, i think it could die, but u SHOULD!


as for type casting in php, just add a "(type)" in front of variable or expression u want to typecast, like this:

$real = 7.5;
$int = (int) $real / 2;

Reply With Quote
  #7  
Old August 21st, 2002, 06:53 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: PHP has a variable as string, and MySQL expects it as interger!

Here is the man for casting and settype().

Reply With Quote
  #8  
Old August 21st, 2002, 06:55 AM
EvilivE EvilivE is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Milwaukee, WI USA
Posts: 291 EvilivE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via Yahoo to EvilivE
RE: PHP has a variable as string, and MySQL expects it as interger!

That was me ... how 'bout those cookies?

Reply With Quote
  #9  
Old August 21st, 2002, 09:45 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: PHP has a variable as string, and MySQL expects it as interger!

Thank you to all.
My problem was on the numbers of fields passed to the query and the number expected by MySQL.
Now I'm asured that PHP and MySQL do convert automatically variables as needed (at least strings to intergers or floats and reversilly).
Freddy

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > PHP has a variable as string, and MySQL expects it as interger!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway