1

I have a mysql table with column "name". in this name i have multiple names, for example id = 1 , and the name = "Ford","Ferrari","Audi","Wolks"

so in id->1 there is multiple names in column "name" and I want all these names in array format. like this below -->

$name = $row['name']; // fetching data from mysql

$show = array ($name);
echo "I like " . $show[0]."<br>";

echo "I don't like " . $show[1]."<br>";

So that I can echo only specific name from unique id..

I hope you're getting what I'm trying to solve.. Thank You.. :)

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 3
    I think you want to use `explode`. You should normalize your table though, https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad As is it is unclear how you'd know what cars you like/dislike. – chris85 Aug 25 '17 at 11:08
  • Sorry I don't want to use explode, as it will count every word. Suppose If there I have stored this value "Maruti Suzuki", "Ford Fiesta" etc.. so I in this case won't get unique names by using this explode. – Naman Bhardwaj Aug 25 '17 at 11:24
  • What is "unique" there? – chris85 Aug 25 '17 at 11:26
  • sorry i was wrong, explode worked for me,, thanks – Naman Bhardwaj Aug 25 '17 at 11:30
  • Sorry again.. :) your idea was to use explode worked now.. – Naman Bhardwaj Aug 25 '17 at 11:30
  • Possible duplicate of [Splitting CSV, but not on comma only (PHP)?](https://stackoverflow.com/questions/11683284/splitting-csv-but-not-on-comma-only-php) – mickmackusa Aug 25 '17 at 13:29

2 Answers2

0

Use the explode function of PHP to get an array of names as like below

For single record

$name = $row['name']; // fetching data from mysql
$names_array = explode(',',$name); //use comma separater

echo "I like " . $names_array[0]."<br>";    
echo "I don't like " . $names_array[1]."<br>";

For multiple records

$names_array = array();
foreach( $rows as $row ) //rows are fetched using mysql query
{
  $names_array = explode(',',$row['name']); //use comma separater

  echo "I like " . $names_array[0]."<br>";    
  echo "I don't like " . $names_array[1]."<br>";
}

Hope so it will help you.

Gokul Shinde
  • 957
  • 3
  • 10
  • 30
0

If the string is stored as:

"Ford","Ferrari","Audi"

Then you can use: str-getcsv.

Although you should consider using Third Normal Form instead.

bashaus
  • 1,614
  • 1
  • 17
  • 33