943,928 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1123
  • PHP RSS
Dec 16th, 2007
0

DB Column contains commas

Expand Post »
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.
Similar Threads
Reputation Points: 22
Solved Threads: 0
Light Poster
scottholmes is offline Offline
27 posts
since Nov 2007
Dec 16th, 2007
0

Re: DB Column contains commas

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.
Last edited by FireNet; Dec 16th, 2007 at 4:56 pm.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Dec 16th, 2007
0

Re: DB Column contains commas

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.
Reputation Points: 22
Solved Threads: 0
Light Poster
scottholmes is offline Offline
27 posts
since Nov 2007
Dec 16th, 2007
0

Re: DB Column contains commas

Try peeking inside the row result with something like this.

PHP Syntax (Toggle Plain Text)
  1. echo "<pre>\n";
  2. print_f($this->result); // or whatever the result set var is called.
  3. echo "</pre>\n";
Reputation Points: 10
Solved Threads: 3
Newbie Poster
MickRip is offline Offline
7 posts
since Dec 2007
Dec 17th, 2007
0

Re: DB Column contains commas

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
Reputation Points: 22
Solved Threads: 0
Light Poster
scottholmes is offline Offline
27 posts
since Nov 2007
Dec 17th, 2007
0

Re: DB Column contains commas

My mistake. It wasn't php or mysql at all. I was adapting a downloaded ajax class that treats a response as an array.
Reputation Points: 22
Solved Threads: 0
Light Poster
scottholmes is offline Offline
27 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: csv, convert date, mysql, php
Next Thread in PHP Forum Timeline: if statements help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC