Reputation: 1335
my problem is with rows mysql_query. I need:
Record 0: 2,text is text,3.23
But I have:
Record 0: 2
Record 1: text
Record 2: is
Record 3: text
Record 4: 3.23
Please help me.
results=($(mysql --user root -proot test -Bse "select id,name from Object"));
cnt=${#results[@]}
for (( i=0 ; i<${cnt} ; i++ ))
do
echo "Record No. $i: ${results[$i]}"
fieldA=${results[0]};
fieldB=${results[1]};
done
Upvotes: 4
Views: 5439
Reputation: 274632
The problem is that you are storing the output of mysql
into an array. Now, if mysql
returns multiple records you won't know when a record ends and the next one starts because the array will contain the "flattened" data e.g. ( record1_fieldA record1_fieldB record2_fieldA record2_fieldB ... )
Instead, use a while
loop to iterate over the records like this:
i=0
while read fieldA fieldB
do
echo "Record $(( i++ )): fieldA: $fieldA fieldB: $fieldB"
done < <(mysql --user root -proot test -Bse "select id,name from Object")
Upvotes: 6