0

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
  • Can you post the source code of the output as viewed by the browser? Commented May 12, 2018 at 20:37
  • Yeah no worries, is there any particular part you want to see? Commented May 12, 2018 at 20:42
  • Apart from the issue you are asking about, I'd really suggest going for using the column names both in the SQL query (e.g. SELECT Description, outcomes FROM Callouts) - to avoid pulling excess data (esp. when the table structure changes in the future) - as well as in the data extraction (as you wrote $Description = odbc_result($dbResult, "Description");), to keep the code working, when the order of the columns changes in the table definition. Commented May 12, 2018 at 20:59

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

$o .= "New string"; is shorter and does the same thing.
Yes you can do that too. @Lou
1

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)

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.