0

I'm a total amateur with php. I've tried dozens of different things, mostly found while browsing around here. I've been at it for hours, and I'm starting to lose my mind. Lacking real knowledge I'm just guessing at this point.

I'm positive this code is completely butchered and will be highly offensive to many of you.

The goal is to display the mssql query results into a nice HTML table. So far, nothing is displaying except the table headers. The query should return dozens of rows.

Here's the current code, any suggestions?

<?php
// Open db connection
$dbc = mssql_connect('host', 'sa', 'password');
if (!$dbc || !mssql_select_db('dbname', $dbc)) {die('Unable to connect or select database!');}

// Select queries
$query = mssql_query("SELECT [name],[customer],[start_time],[end_time],[status] FROM dbo.reservation ORDER BY last_update DESC");

// display the results!
if (!mssql_num_rows($query)) {
    echo 'No records found';
} else {
    ?>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Customer</th>
                <th>Start Time</th>
                <th>End Time</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
        <?php 
            while ($row = mssql_fetch_row($query)) {
                echo'<tr>'; 
                echo'<td>'. $row['name']."</td>";
                echo'<td>'. $row['customer'].'</td>';
                echo'<td>'. $row['start_time'].'</td>';
                echo'<td>'. $row['end_time'].'</td>';
                echo'<td>'. $row['status'].'</td>';
                echo'<tr>';
            }
        ?>
        </tbody>
    </table>
    <?php 
}
?>

Note: This is for an internal site and I need to use mssql_connect.

3
  • 1
    Sounds like your query is not returning any data. Suggest using var_dump($query) to see just what you are getting back. Commented May 27, 2015 at 23:37
  • that returns: resource(3) of type (mssql result). Not sure what that means. Also, if I run the same query in MSSQL, I get results back. Commented May 27, 2015 at 23:40
  • Well, that sounds like you got three result rows. I think your problem is that you are using mssql_fetch_row() when what you really want is mssql_fetch_array() Commented May 27, 2015 at 23:49

1 Answer 1

1

You are trying to index the result row from mssql_fetch_row() with column names, but this function only returns an array with numeric indices. Use mssql_fetch_array() to get an array that you can index with column names.

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

1 Comment

Great! Now you should accept the answer - as the one who asked the question you should see a checkmark below the up and down arrows next to the answer - click on that to indicate that this answer worked for you. If you ask a question and get multiple answers you should accept the one that you think is best.

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.