0

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

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
nishant
  • 3
  • 4
  • 1
    You cant. Thats what arrays are for – Felix Oct 26 '17 at 13:23
  • [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a class for](https://github.com/GrumpyCrouton/GrumpyPDO) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](https://phpdelusions.net/pdo/mysqli_comparison) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Oct 26 '17 at 13:33

1 Answers1

0

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