1

I need to get all column names and values in the row where id = $id.

Column names should be echoed as title; values should be echoed as story

Something like this:

function story($id) {
    global $db;
    $sql = "select *  from users where id = :aid limit 1";
    $st = $db->prepare($sql);
    $st -> execute([":aid" => $id]);
    $row = $st->fetch();
    $sql = // select all column names;
    $columns = // array - result of $sql
    $story = "";
    foreach ($columns as $el) {
        $i = array_search($el, $columns);
        $val = $row[$i];
        $story .=
        "<div class='title'>" . $el . "</div>\n" .
        "<div class='story'>" . $val . "</div>\n";
    }
    echo $story;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Why are you using `array_search()`? Build the resultset that you want to work with. I recommend passing `$db` as a function parameter instead of declaring `global`. – mickmackusa Sep 24 '18 at 04:56
  • @mickmackusa, because I need that index to get appropriate element in `row` array; –  Sep 24 '18 at 04:57
  • `->fetch(PDO::FETCH_ASSOC)`? – mickmackusa Sep 24 '18 at 04:59
  • @mickmackusa, sorry, can't understand, where should I write that? –  Sep 24 '18 at 05:01
  • 1
    If you just do: `$row = $st->fetch(PDO::FETCH_ASSOC);`, you will get the row as an associative array. No need for anything else. [Here's the manual](http://php.net/manual/en/pdostatement.fetch.php) – M. Eriksson Sep 24 '18 at 05:01
  • I feel like you table design may be "questionable" if you are using column names as part of your output. What kind of schema are we looking at here? – mickmackusa Sep 24 '18 at 05:02

3 Answers3

3

No need to do array_search(), do like below:-

function story($id) {
    global $db;
    $sql = "select *  from users where id = :aid limit 1";
    $st = $db->prepare($sql);
    $st -> execute([":aid" => $id]);
    $row = $st->fetch(PDO::FETCH_ASSOC);
    if(count($row)>=1){
      foreach ($row as $column => $value) {
        echo "<div class='title'>" . $column . "</div>\n" .
        echo "<div class='story'>" . $value. "</div>\n";
      }
    }
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

I recommend that you pass the db connection as a function parameter instead of using a global declaration.

fetch(PDO::FETCH_ASSOC) returns a one-dimensional associative array or false.

http://php.net/manual/en/pdostatement.fetch.php

Using capitalization on your sql keywords can improve readability.

I have removed the LIMIT clause, but if your id column is the PRIMARY KEY, the limit won't matter.

function story($db, $id) {
    $st = $db->prepare("SELECT * FROM users WHERE id = :aid");
    $st->execute([":aid" => $id]);
    if (!$row = $st->fetch(PDO::FETCH_ASSOC)) {
        echo "no row found";
    }
    foreach ($row as $key => $val) {
        echo "<div class='title'>" . $key . "</div>\n";
        echo "<div class='story'>" . $val . "</div>\n";
    }
}

I do have a suspicion that your database table design may not be optimal. Typically you'll want a stable column structure that allows your row count to increase. If your table is expanding both vertically and horizontally, I wonder what your requirements are.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • what is the difference ? –  Sep 24 '18 at 05:14
  • `global` scoped variables are not recommended versus passing the connection as a parameter into the custom function. Also, the `count()` on the row is not necessary. If `id` is a primary/unique key (and it usually is in properly designed tables), then `LIMIT 1` is not necessary. – mickmackusa Jan 07 '20 at 06:17
-2

You're approaching it a bit wrong... When a $sql returns with mysqli as an associative array, it returns the column name as the key...

hence:

$columns = // array - result of $sql as an assoc array
$story = "";
foreach ($columns as $key => value) {
    $story .=
    "<div class='title'>" . $key . "</div>\n" .
    "<div class='story'>" . $value . "</div>\n";
}
Guy Louzon
  • 1,175
  • 9
  • 19