DB Column contains commas

Thread Solved

Join Date: Nov 2007
Posts: 23
Reputation: scottholmes is an unknown quantity at this point 
Solved Threads: 0
scottholmes scottholmes is offline Offline
Newbie Poster

DB Column contains commas

 
0
  #1
Dec 16th, 2007
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.
Scott Holmes
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: DB Column contains commas

 
0
  #2
Dec 16th, 2007
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.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 23
Reputation: scottholmes is an unknown quantity at this point 
Solved Threads: 0
scottholmes scottholmes is offline Offline
Newbie Poster

Re: DB Column contains commas

 
0
  #3
Dec 16th, 2007
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.
Scott Holmes
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: MickRip is an unknown quantity at this point 
Solved Threads: 3
MickRip's Avatar
MickRip MickRip is offline Offline
Newbie Poster

Re: DB Column contains commas

 
0
  #4
Dec 16th, 2007
Try peeking inside the row result with something like this.

  1. echo "<pre>\n";
  2. print_f($this->result); // or whatever the result set var is called.
  3. echo "</pre>\n";
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 23
Reputation: scottholmes is an unknown quantity at this point 
Solved Threads: 0
scottholmes scottholmes is offline Offline
Newbie Poster

Re: DB Column contains commas

 
0
  #5
Dec 17th, 2007
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
Scott Holmes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 23
Reputation: scottholmes is an unknown quantity at this point 
Solved Threads: 0
scottholmes scottholmes is offline Offline
Newbie Poster

Re: DB Column contains commas

 
0
  #6
Dec 17th, 2007
My mistake. It wasn't php or mysql at all. I was adapting a downloaded ajax class that treats a response as an array.
Scott Holmes
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC