foreach question

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 8
Joined: Fri Aug 17, 2018 12:57 pm

Tue Oct 22, 2019 12:57 pm

In this example, how would I obtain the first column, since it has spaces in it. I've tried all kinds of things like brackets, parenthesis, quotes, etc. but have not yet figured it out and cannot find any examples on the web since the examples on the web tend to be very simplistic.

Code: Select all

  $dbh = new PDO('sqlsrv:server=;database=WhirlyBird', $user, $pass);
  $result = $dbh->prepare("SELECT [int co id],[int co id] ID,[int co Company Name] [company] FROM [Company] WHERE LEN([int co Company Name]) > 1");
  $rows = $result->fetchAll(PDO::FETCH_OBJ);  
  foreach ($rows as $row) {
    echo "<option value=".$row->ID.">".$row->company."</option>".PHP_EOL;

User avatar
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Wed Oct 23, 2019 8:40 am

Use SQL to fetch the column, there's no need to use a foreach loop.
Post Reply