When I use select indicator...in my select statement below I can't use $row[indicator] when I use select indicator as indicator I can use $row[indicator]. Why is this?

$stmt=$mysqli->prepare("select indicator as indicator, count(status)as 'ok', (select count(status) from viewindicatoren where status ='niet ok' and afdelingid=? group by IND_id ) as 'nietOk' from viewindicatoren where status='ok' and afdelingid=? group by IND_id");
        $stmt->bind_param("ss",$afdelingID,$afdelingID);
        $stmt->execute();

        $res=$stmt->get_result();    
        while ($row=$res->fetch_assoc()){
          echo "indicator: $row[indicator] ok: $row[ok] niet ok: $row[nietOk]";

        }

Recommended Answers

All 4 Replies

Member Avatar for diafol

Why as you providing a tablename with the same alias?

I tryed it with an alias, in this case "indicator", it might as well be "t". The problem is that I have to use an alias for it to work. In other cases I never had to do that.

Member Avatar for diafol

I think you only have to provide an alias for the derived table, no the field 'indicator'. Anyway, ensure that you enclode your fieldnames in backticks.

The status thing is a bit confusing as it looks as though you're storing strings? If so, why not use 0,1 (0 = notOK, 1= OK)?

Solved it, apparently it's called Indicator in the db, I thought php looks at the select statement instead of the column name the db returns.
Stupid mistake but I learned something of it.
Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.