How do I retrieve data from a database using PHP?

advertisements

I have a problem with retrieving data from a database using PHP statements. At the moment the output looks like:

[{"Bez":"Cino"},{"Bez":"Extra"},{"Bez":"Panini"},{"Bez":"And so on"}]

"Bez" is the label of the column and we don't want it to be displayed. We just want to display the cell's contents so it should look like:

[Cino,Extra,Panini,And so on]

The php is:

<?php
  $username = "root";
  $database = "kaffeehaus";
  mysql_connect('localhost', $username);
  @mysql_select_db($database) or die("Geht nicht!");

  $query = "SELECT Bez FROM kategorie";
  $result = mysql_query($query) or die(mysql_error("error"));
  $num = mysql_numrows($result);

  mysql_close();

  $rows = array();
  while ($r = mysql_fetch_assoc($result))
  {
    $rows[] = $r;
  }

  echo json_encode($rows);
?>


Replace

$rows[] = $r;

With

$rows[] = $r['Bez'];

...though to be honest, I'd replace the whole "fetch" part with:

while($r = mysql_fetch_row($result))
    $rows[] = $r[0];

That's because mysql_fetch_row is generally faster than mysql_fetch_assoc.