简体   繁体   中英

How to get multiple string data from mysql into an array format

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.. :)

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.

If the string is stored as:

"Ford","Ferrari","Audi"

Then you can use: str-getcsv .

Although you should consider using Third Normal Form instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM