I have a mysql database with user input fields. Some of these varchar fields contain commas as part of the data. When I try to display these fields they are treated as arrays and display only the first "element". This occurs both with mysql_fetch_row() and mysql_fetch_array(). I'm chagrined I haven't run into this before, perhaps because commas have generally been limited to text fields. Any help on this would be appreciated.

use the function mysql_escape_string();

It will make sure all the characters are escaped and safe to use in a mysql query

escaping a character is adding a \ in front of a character. Eg: \'


Hope that fixes your problem.

mysql_escape_string() doesn't solve my problem. The database field (bdyprt varchar(75)) contains the string "Neck, back, shoulders, dizziness and headaches" (I've quoted it here for clarity).

I fetch a row, thus: $res = mysql_fetch_row($this->res); or even as
$this->ap80rec = mysql_fetch_assoc($this->res);
echo $res[4] ; displays Neck
Either mysql or PHP is treating $res[4] as an array and is only giving me the first element.. I'm obviously missing something as phpMyAdmin fetches and displays the string correctly.

Try peeking inside the row result with something like this.

echo "<pre>\n";
print_f($this->result); // or whatever the result set var is called.
echo "</pre>\n";

As I'd mentioned, it prints the first assumed element in what it thinks is an array (Neck). If I change all the commas in the field to semicolons, the data displays fine. Most of my earlier web app work was done using Postgresql and I don't recall ever having this problem so I rather suspect something on the mysql side. Also, I'm trying this with PHP5 - in case that makes any difference.

$res[4]: Neck; back; shoulders; dizziness and headaches

My mistake. It wasn't php or mysql at all. I was adapting a downloaded ajax class that treats a response as an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.