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

mysql_query with quotes around variables?

I run the code below with cli (no web server)
it works fine on my xampp setup but will not work on my php standalone setup on another machine.
Expand|Select|Wrap|Line Numbers
  1. $var1 = "-1";
  2. $var2 = 7;
  3. $q="update `table1` set `fld1` = '$var1' where `fld2` = '$var2'";
  4. mysql_query($q);
  5.  
I am not getting any errors (yes error reporting is on), it just wont update the row.
it works if I type the sql straight into mysql query browser.
Expand|Select|Wrap|Line Numbers
  1. update `table1` set `fld1` = '-1' where `fld2` = '7';
  2.  
the funny thing is, if I remove the single quotes from the variables it will work.
Expand|Select|Wrap|Line Numbers
  1. $q="update `table1` set `fld1` = $var1 where `fld2` = $var2";
fld1 is TINYINT(1) with unsigned unchecked
fld2 is INTEGER()
I thought php can convert strings into the appropriate data type if needed.
any ideas why its working on xampp and not on a php standalone install?
Thanks for any help!
Jan 4 '09 #1
4 4534
Dormilich
8,658 Expert Mod 8TB
@giloosh
it does, but the problem has a different origin (i.e. it is out of the scope of PHP).

any number types shouldn't be quoted in SQL. but you wrote the quotation marks in the string yourself.... if you use any DAL (database abstraction layer) you can use that to handle quoting for you.

regards
Jan 4 '09 #2
Atli
5,058 Expert 4TB
Hi.

Are there any differences between the MySQL servers on those two machines, or the PHP versions?

Try catching the error:
Expand|Select|Wrap|Line Numbers
  1. mysql_query($q) or die(mysql_error());
  2.  
See if that tells you anything.

You might also want to consider upgrading to the Improved MySQL extension.
The old MySQL extension was written for MySQL 3, and while it does work with the later versions, it's a bit outdated.
Jan 4 '09 #3
thanks for the replies. All very useful information.
heres the mysql error I would get:
truncated incorrect double value php mysql
the problem had nothing to do with wraping the variabl in quotes. The variableI was using was extracted from a text file and sent in through a socket over the internet, and somewhere along the way they picked up "whitepsaces" of some sort. When I would echo the $var2 it would show up fine, and the white spaces were invisible to me, so I had no idea. after doing trim($var2) it solved the problem. very weird :-/
Jan 5 '09 #4
Atli
5,058 Expert 4TB
Ahh ok. You should be very careful when using "foreign" data like that. There is always the chance that the data is corrupt, or even that it has been altered purposefully to damage your web.

Consider, for example, if somebody had managed to send this as the value for $var2:
Expand|Select|Wrap|Line Numbers
  1. 1' or 1='1
PHP has some very useful functions to validate data like this.
Like the is_numeric function, which would be ideal for your situation.

Also, remember to always pass all data through the mysql_real_escape_string function before putting it into a query.
Jan 5 '09 #5

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

Similar topics

4
by: Josh | last post by:
I have this block of code.... while($i <= $count) { $final = mysql_fetch_row($result); $blocknamelist .= "<a style = \"cursor: pointer\" onmouseout = \"clear_desc()\" "; $blocknamelist .=...
13
by: lawrence | last post by:
A user writes this sentence: "It was the New Urbanist's nightmare of sprawl run amok." They input that and my PHP script hits it with addslashes() and then the sentence gets put in the database....
6
by: aa | last post by:
I use the following fragment of code to output datf from MySQL: ====================================================== $chan = mysql_connect ($db_host, $username, $password); mysql_select_db...
2
by: asd987 | last post by:
Hi, I use document.write in a HTML-document to display same information. Now I have a problem displaying text that sometimes contains two kinds of quotes simultanelisly. I don't know on...
7
by: John Moore | last post by:
Hi I posted three days ago about a function I wrote which kept refusing to run a mysql_query. Well I did away with the function, and hard coded the variables into the query itself. Here's...
4
by: (PeteCresswell) | last post by:
Is his just a flat-out "No-No" or is there some workaround when it comes time for SQL searches and DAO.FindFirsts against fields containing same? I can see maybe wrapping the value searched for...
1
by: veaux | last post by:
I'm using Access query and have to spit out a text file to load into a 3rd party program. The text file has to include quotes (" ") around certain infomation. The string I have is below. ...
14
by: adam.timberlake | last post by:
This is a really basic question for all you people out there who know PHP. This is not a problem but just something I'm confused about. I was reading the article below and wondered why are normal...
2
by: ncsthbell | last post by:
I am trying to construct a 'Copy' statement in Access 2000 vb that I will actually write out to create a .bat file. My problem is that I need to put quotes around the vcFileFrom and vcFileTo...
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: 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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.