2

I currently have a table called category in my database

category

catId categoy

  1. News
  2. HTML
  3. PHP
  4. CSS

Trying to pull back off rows from the database only brings back the 1 result using the following code

    private function navigation(){
        $url = BASE_URL;
        $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        if (!$dbc) {
            trigger_error('Could not connect to MySQL:' .mysqli_error());
        }
    $query = "SELECT catId, category AS 'catName' FROM category ORDER BY catName DESC";
        $result = mysqli_query($dbc, $query) or trigger_error("Query: $query\n<br>MySQL Error: ");
        while($row = mysqli_fetch_array($result)){
            $name = $row['catName'];
            $catId = $row['catId'];
            $this -> nav =<<<NAVIGATION
            <li>$name</li>
            <p>test</p>

NAVIGATION;
       }
    }

I have tried doing it outside a function and all rows are brought back without problem.

$querying = "SELECT catId, category AS 'catName' FROM category ORDER BY catName ASC";
        $results = mysqli_query($dbc, $querying) or trigger_error("Query: $query\n<br>MySQL Error: ");
        while($row = mysqli_fetch_array($results)){
            $name = $row['catName'];
            $catId = $row['catId'];

            $page->body ("$name");
        }

I was wondering if someone could guide me in where I am going wrong.

Thank you in advance.

Community
  • 1
  • 1
  • Where are you expecting multiple results? `$this->nav` and `$page->body` is overwritten on every iteration. – alexn Mar 22 '12 at 16:49

2 Answers2

0

On first code you everytime assign value to $this -> nav variable so it gives you last data from the array of mysql result. And its not true use database connect everytime when you call function.

safarov
  • 7,793
  • 2
  • 36
  • 52
0

I would assume you want to append each row to the previous rows. Try

$this -> nav .= ...

(dot equals)

SenorAmor
  • 3,351
  • 16
  • 27