donglei1699
2018-05-12 20:35
浏览 60
已采纳

php数据显示在表格中

Evening everyone,

I'm in need of a little bit of help.

I've got a bit of quite basic PHP coding which is fetching data from a table in a MS SQL database and have come unstuck.

I'm trying to get the data to display in a table and regardless of what I do it just won't do it, the data is slapped on the page in a scrambled mess with no table headings at all (as if the browser isn't registering the table code).

<?php

$databs ='C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\MRT.mdf';

   $dbConnect = odbc_connect("MRTServ","","")
        or die("Error opening database .... use the browsers BACK button");

  if ($dbConnect)
    $dbQuery = "SELECT * FROM Callouts";
    $dbResult = odbc_exec($dbConnect,$dbQuery);


  while (odbc_fetch_row($dbResult)) {
     $Description = odbc_result($dbResult,3);    //third column
     $outcomes = odbc_result($dbResult,5);      //fifth column


        //or could retreive data by specifying the column field names 
        //specified in query
      //$Description = odbc_result($dbResult, "Description");
      //$outcomes = odbc_result($dbResult, "outcomes");
 echo "<br />";
 echo "$Description, $outcomes";
                                }
     $o = '<div id=ATtable>
           <table-id-"table1">
             <thead>
                <tr>
                    <th>Description</th>
                    <th>Outcomes</th>
                    </tr>
                </thead>
                <tbody>';

    while ( $record = odbc_fetch_array($dbResult) )
      {
          $o='<tr><td>'.[$Description].'</td><td>'.[$outcomes].'</td></tr>';

      }               

   $o='</tbody>
       </table>
       </div>';
    echo $o;

  odbc_close($dbConnect);

?>

This is the PHP code I've come up with but for the life of me cannot see where I've made the mistake or missed something.

Please can someone give me a nudge in the right direction?

Thanks, Dan

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanqiongchong0354 2018-05-12 20:41
    已采纳

    You are not concatenating your $o html code, use $o .= (notice the . (dot) used for concatenating the html code), in stead of $o = (which just overwrites your variable every time)

    点赞 打赏 评论
  • dongqi9125 2018-05-12 20:48

    Whenever you are assigning a new value to o the old strings is replacing. To solve this problem simply use

    $o=$o . “New string”
    

    The . will combine the old string with new one.

    点赞 打赏 评论
  • douhuang7263 2018-05-12 20:56

    It seems that you want to build the HTML and output it. As other pointed out, instead of building it, you are replacing the previous "brick" with a new one on each assignment to the $o variable. You can go with the suggested fixes, which will actually allow you to build the HTML in the variable, but I'd just suggest to ditch the variable completely, and use echo instead, to output the HTML on the fly.

    PS.: I'd suggest to wrap the $Description and $outcomes variables with a call to htmlspecialchars, so your HTML still works, when the data in the DB contains something messing with it, (e.g. a string "0 < a"). Otherwise you get an XSS vulnerability right there (unless you made sure to sanitize the data, when it comes into the DB, but I'd still encode the values, to be rather safe, than sorry, when something mean slips through your sanitization).

    点赞 打赏 评论

相关推荐 更多相似问题