2

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);
?>

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. It worked just like it should be ;) . CI
0

Replace

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

with this:

while ($r = mysql_fetch_array ($result, MYSQL_ASSOC)) { $rows[] = $r['Bez']; }

Comments

0

Use mysql_fetch_row() instead (or mysql_fetch_array($result, MYSQL_NUM);). It fetches a numerical array so the column names are not shown.

Then, your output will look something like this:

[{"0":"Cino"},{"1":"Extra"},{"2":"Panini"},{"3":"And so on"}]

Comments

0

Remove the json_encode part:

echo json_encode($rows); ?>

And use bez as row key:

echo ($rows['bez']); ?>

Comments

0

And how does it work if there are more than one cell?

Our php looks like

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

 $ID_Kategorie = $_GET["Kategorie"];

 $query = "SELECT * FROM artikel WHERE ID_Kategorie=$ID_Kategorie";


 $result=mysql_query($query) or die("error");
 $num = mysql_numrows($result);

 mysql_close();

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

 }
 echo json_encode($rows);    ?>

And the output like:

[{"ID_Artikel": "12","ID_Kategorie":"19","Bez":"blabla","Beschreibung":"blabla"}]

But it schould look like:

[{"12","19","blabla","blabla"}]

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.