Skip to content
Advertisement

How to retrieve all rows from an SQL table into an array?

I am trying to retrieve 4 rows and I expected that It should display all 4 rows but it’s only displaying the first row. This is the code I am using

$coquery = "Select distinct coName from avgcarcompany";
$crun = mysqli_query($con,$coquery);

$arrey = mysqli_fetch_assoc($crun);

print_r($arrey); 

Advertisement

Answer

If you want to get all rows from the result set then you need to fetch all. Right now you are fetching only one.

To fetch all rows use fetch_all()

$coquery = "Select distinct coName from avgcarcompany";
$crun = $con->query($coquery);

$arrey = $crun->fetch_all(MYSQLI_ASSOC)

print_r($arrey);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement