简体   繁体   中英

how to store the data into different string variables from array at run-time?

I am executing sql query in phpmyadmin.

Here I have faculty table in database which has Fname and Fdept fields.

My PHP file is:-

include("configuration.php");
$dept = $_GET['Dept'];

$sql = mysqli_query($con,"SELECT Fname from faculty WHERE Fdept = '$dept'");
$data = "";
while ($row = mysqli_fetch_assoc($sql)) {
    $data = $data.$row['Fname'].":";
}
$data1="SUCCESS:".$data;
echo "{'query_result':'$data1'}";
?>

So, here I am fetching the faculty names by referring their department..

query_result returns data of faculty names.

example:

query_result:SUCCESS:John:Nishant:Nawaz

by that i can split each name using split method.. like:

String[] parts = query_result.split(":");
for(int i=0; i<parts.length; i++)
 {
   System.out.println(parts[i]);
 }

output :

SUCCESS
  John
  Nishant
  Nawaz

But how can I assign each name to different string variables, without using index like

String a = part[0];
String b = part[1];
String c = part[2];

Because query result size will be dynamic and if i use this this there will be a ArrayIndexBoundException occurs So, how can I use that data by storing it into different string variable, by that I can put each variable to sharedPreferences and use it in other activities

you will need to wrap your declared variable name in {} as follows

${"prefix" . $i} = part[$i];

now your preferred language is ambiguous in this question, but you can't dynamically declare variable names in java, so you're stuck with php if you really want/need this. i don't exactly see what limitations you face with just using an array, but i'm not in your shoes. :)

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