Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Commas and MySQL statements

Status
Not open for further replies.

phplilo

Programmer
Jan 25, 2005
3
GB
Lo people,

I am new here as you can prolly tell by my post count :) tend to hang over at highrankings a lot, and was pointed this way from a mod there (hi Chris).

Anyway - I have come across a prob with mysql queries - when I submit say a string to a varchar field, that contains a comma, I get a mysql error for example:

$query = "INSERT INTO sometable (title) VALUES ('Hello, I am a fish')";
mysql_query($query);

Now short of parsing the mysql data replacing commas with spaces etc, does anyone know whats going wrong here?

Cheers

LiLo
 
You could try an alternate format for insert:
Code:
$q = "insert into sometable set title='Hello, I am a fish'";
$r = mysql_query($q);
if (!$r) echo "Problem with query &lt;$q&gt;<br>".mysql_error();
Commas shouldn't be a problem.

Ken
 
Try:

$sql = "INSERT INTO yourtable(cname)
VALUES ('Hello, I am a fish')";
$result = mysql_query($sql);

Other than that check the field size for the table/column receiving the data. Make sure it's set to atleast field_type(18) <-- just big enough for the example used/provided

 
Yes, commas should not be a problem.. if they are something else is wrong.

However, to help keep you going forward you can use the following to help alot with debugging.

Code:
mysql_query($query) or die('<pre>Error in sql:'.$query.'<br />'.mysql_error());

And as a side note, you may find the following function very useful for whatever is actually causing a problem in your query.

If your example is really an example of one going bad, I'm assuming title is a reserved word in MySQL... so you need to enclose it in backquotes. (Note these are not single quotes)

Code:
$sql = "INSERT INTO sometable (`title`) VALUES ('Hello, I am a fish')";
mysql_escape_string()
 
Cheers for the help, after more scrutinising of my code working with the fact that commas were not causing the prob, I have narrowed it down to that I was creating an array from the data and searching it etc... and I was using the comma seperator to split!

So... doh.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top