1

Fetch the data in array format like :

$value = array ("ABC","XYZ","OPQ");

$query = "SELECT * FROM  designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
    $designation= $row["designation"]; 
}

Need Result:

$value = array ("ABC","XYZ","OPQ");
8
  • I assume since you loop, it should be $designation[] = $row["designation"]; to add the values to the array and not overwrite on each iteration. Commented May 2, 2019 at 12:00
  • but how to echo in this same format [$value = array ("ABC","XYZ","OPQ");] Commented May 2, 2019 at 12:01
  • You want to echo an array as a string? In what purpose? There are better ways to output arrays. What is the user supposed to use the data as? Commented May 2, 2019 at 12:03
  • 1
    And what's your question? Why not write some code that returns the expected structure? Commented May 2, 2019 at 12:04
  • 2
    This is a rabbit hole. Think your question through and ask it as clearly as you can. What do you have and what is the end result. Not what you want part way. The end result. Commented May 2, 2019 at 12:07

2 Answers 2

1

Fetch the designation column, and push it into an array with the syntax $array[] = $value.

$designation = [];
$query = "SELECT designation FROM  designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
    $designation[] = $row["designation"]; 
}
print_r($designation);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Qirel getting result in following format : Array ( [0] => ABC [1] => XYZ [2] => OPQ) but I want result in same format array ("ABC","XYZ","OPQ");
That is the same. When you print it, you show the value of the keys as well, but it is in fact the same. To prove it, see this test: 3v4l.org/GOQSR
Got it, Thank you @Qirel
0

if the "ABC","XYZ","OPQ" are the same field [designation], you can convert $designation to array.

$query = "SELECT * FROM  designation_master";
$result = mysqli_query($mysqli, $query);
$designation = array();
while ($row = mysqli_fetch_array($result))
{  
    $designation[] = $row["designation"]; 
}

4 Comments

I am trying to print array like print_r($designation); but not getting exact same result
array(0=>"ABC", 1=> "XYZ",2=> "OPQ") is same array ("ABC","XYZ","OPQ"). the index is bulit.
how to remove indexing from array and add double quote into it. ?
PHP Array always have index, but you can assign custom index name. you don't need quote it, if it is number index, you can access the value with integer or string, like $designation[1] or $designation["1"]. And if you want custom key name, you can set it as the following syntax: $designation['key1"] = "value1";

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.