Yuniru'
Yuniru'

Reputation: 13

Display all the items of a column?

I'm a Mysql/php novice and I'm trying to get several results of one query. I have a db named "my_db", who has a table named "my_table". This table has only one column named "fruits". I inserted some elements in items like banana, pineapple, coconuts and apple.

I would like to display all this items so I tried this code:

$sql = 'SELECT items FROM my_table';
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br />'.mysql_error());
$data = mysql_fetch_array($req);
mysql_free_result ($req);
echo $data['items'];

But this only show me the first element of the column "items" (here, banana). How can I show a list of all the elements ?

Thanks in advance !

Upvotes: 0

Views: 50

Answers (2)

NormundsP
NormundsP

Reputation: 413

If you use mysql, then you need to create cycle, for excample "while cycle":

$sql = mysql_query("SELECT * FROM lietotajs");
  while($row = mysql_fetch_array($sql ))
        {
            echo $row['ID'];
        }

Upvotes: 1

Waleed Ahmed Haris
Waleed Ahmed Haris

Reputation: 1244

You have to run while loop after running the query, other wise it will restrict to single. Try to do this, not tested. Hopes it will help

$sql = 'SELECT items FROM my_table';
$req = mysql_query($sql) or die('Erreur SQL !<br />'.$sql.'<br/>'.mysql_error());
while($data = mysql_fetch_array($req)){
echo $data['items'];
}

Upvotes: 0

Related Questions