0

categoryID
   10
   20
   30

For example. Above is my categoryID column with values of 10, 20, and 30 PHP MySQL. What I want to do is to echo those values using an array. Like -> 10 20 30. Below is my code. In my code. I just stored all row data from categoryID into my $array variable. My concern is. How do I echo all the values?

Thank you for the help!

<?php
include ("dbconnect.php");

$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);

$array = array();

while($row = mysqli_fetch_assoc($result)) {
   $array[] = $row;
}

?>

2 Answers 2

1

Use $array variable as below:-

$array[] = $row['categoryID'];

and to print the array try:-

print_r($array);

and to print them as 10 20 30 try the following:-

echo implode(' ',$array);
Sign up to request clarification or add additional context in comments.

Comments

0

The best way is to use json_encode() function

echo json_encode($array);

so try this

<?php
include ("dbconnect.php");

$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);

$array = array();

while($row = mysqli_fetch_assoc($result)) {
   $array[] = $row;
}


    echo json_encode($array);
    exit;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.