I am running a query on MySQL table...the query works, on line 18 in the code below I start a while loop with
while ($row = mysqli_fetch_assoc($result))
I then run some code to make alternate styles of <tr class="odd"> or <tr class="even"> so I can apply some CSS styles to the table to show shading in background.
Then I have the row where I out put the fetched results on line 26-27. I was surprised that when I used the $row array descriptor in an echo command with double quotes it doesn't interpret the value... If written like this:
echo "<td>$row['user_id']</td><td>$row['first_name'] $row['last_name']</td>";
The line fails...
But if I break the variables out of the double quotes and add a dot to concatenate like this:
echo "<td>".$row['user_id']."</td><td>".$row['first_name'] ." ".$row['last_name']."</td>";
Then it works.
I thought variables were interpreted inside double quotes so why does this not work here?
And is there an easier way to output these results such that I can wrap them with a html td tag in the process? Right now I am only using a couple columns to test but soon that query will be expanded to about 15 fields I want to put into a table.
Any suggestions on a better way to write it than in line 26-27?
1 if (!$_SESSION['valid_user'])
2 {
3 echo "<h2>You are not logged in to run query!</h2>";
4 exit;
5 }
6 else
7 {
8 $query = "select user_id, last_name, first_name from directory ";
9 echo "<p>Running query for ".$_SESSION['valid_user'] .":<br /> $query </p>";
10 }
11 $result = mysqli_query($conn, $query);
12 $num_results = mysqli_num_rows($result);
13 echo "<p>Number of rows: $num_results<br>Returning results:</p>";
14 $style="odd";
15 echo "<table>\n";
16 echo "<tr><th>Id</th><th>Name</th></tr>\n";
17
18 while ($row = mysqli_fetch_assoc($result))
19 {
20 if ($style=="odd") //set rows as even and odd
21 $style = "even";
22 else
23 $style = "odd";
24 echo "<tr class=\"$style\">";
25
26 echo "<td>".$row['user_id']."</td><td>".$row['first_name']
27 ." ".$row['last_name']."</td>";
28 echo "</tr>\n";
29 }
30 echo "</table>\n";