Reputation: 189
I have a database and I would like to fetch a column in the form of a numeric array, if possible.
This is my code, but I am only able to get the first item.
<?php
$dbc = mysqli_connect('localhost', 'starCreator', 'starCreatorPass','starsystem') or die ('starCreator: ... I couldn.t connect to the database, something is wrong with the authentication'.'<br />');
if ($dbc)
{
echo 'starCreator: ... connection to the database is established'.'<br/>';
}
$query = "SELECT name FROM stars";
$result = mysqli_query($dbc,$query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
echo $row[0];
echo $row[1];
?>
This row[1] doesn't retrun the second item.
Upvotes: 0
Views: 1031
Reputation: 925
mysqli_fetch_array
only gets 1 row. The index of the row represents the columns, not the different values. To get the other values, you need to call mysqli_fetch_array
multiple times, preferably in a loop :
while($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
echo $row[0];
}
Upvotes: 6
Reputation: 3027
You need to loop through the results (rows)
$query = "SELECT name FROM stars";
$result = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
echo $row[columnName];
echo $row[anotherColumnName];
}
Upvotes: 3