Tom Davison
Tom Davison

Reputation: 41

SQL Server Query killing script - No errors (PHP)

I'm running a simple PHP script to take some queries from a remote SQL Server. I have 3 queries in total, and 1 MySQL query. The first SQL Server query works fine, returns what I expect and so onto the next, $orders. Here the script stops dead. I mean no further output is rendered to the browser, the view-source of the result just stops at before the block of PHP starting with the $orders query.

There are no errors in my apache log, error_logs or anywhere else I can find. I've also displayed get_last_message for mssql and it displays nothing. ANYTHING typed or echo'd after the second mssql_query(); is not working and the script just stops. There is no timeout and it is very quick to load. I've run the same query manually in SQL Server and it returns a result.

Help?

            <?php

        // CHECK IF USER LOGGED IN
        session_start();
        if ($_SESSION['loggedIn'] != "true") {
             header("Location: /reps/");
        }

        // GRAB DATABASE DETAILS
        require('../assets/mssql_connect.php');
        require('../assets/connect.php');

        // SET GLOBAL REP VARIABLES
        $rep = $_SESSION['name'];
        $admin = $_SESSION['id'];
        $customer = $_GET['id'];
        $code = $_POST['Code'];

        ?>

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <link rel="stylesheet" type="text/css" href="../assets/css/dashstyles.css" />
            <script language="javascript" src="/reps/assets/js/formvalidation.js"></script>
            <title>Reps Dashboard - <?php echo $rep; ?></title>
        </head>

        <body>

        <?php 

        // GET HEADER INCLUDE
        require('../assets/header.php');


        // BUILD QUERY USING ID TO GET FIELDS
         $edit = mssql_query("SELECT * FROM Customers WHERE ID='$customer'")
         or die(mssql_get_last_message());

          while ($row = mssql_fetch_array($edit)) {
            $code = $row['Code'];
            $name = $row['Name'];
            $address = $row['Address'] . "<br />" . $row['Town'] . "<br/>" . $row['County'] . "<br />" . $row['Postcode'];
            $phone = $row['Tel'];
            $fax = $row['Fax'];
            }

        mssql_free_result($edit);

        ?>

            <div id="main">
            <p class="welcome">Customer Detail: <?php echo $name; ?></p><p class="backlink"><img src="../assets/images/icons/back_icon.png" alt="Back" />&nbsp;<a class="backlink" href="/reps/dashboard/">Back to dashboard</a></p>
                <table class="data">
                    <tr>
                        <td style="width:40%">
                            <table class="data" style="width:100%">
                                <tr class="theader">
                                    <td>Customer Details</td>
                                </tr>
                                <tr>
                                    <td><p><b>Customer Code: </b><?php echo $code; ?></p>
                                        <p><b>Address:</b><br /><?php echo $name . "<br />" . $address; ?></p>
                                        <p><b>Tel: </b><?php echo $phone . "<br /><b>Fax: </b>" . $fax; ?></p>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style="width:60%">
                            <table class="data" style="width:100%">
                                <tr  class="theader">
                                    <td>Customer Orders History</td>
                                </tr>
                                <tr>
                                    <td>
        <?php

        require('../assets/mssql_connect.php');

        // BUILD NEW QUERY FOR ORDERS
        $orders = mssql_query("SELECT TOP 10 * FROM MainJobDetails WHERE InvoiceCustomerCode='$code'")
        or die(mssql_get_last_message($orders));

        // CHECK FOR 0 RECORDS
        if(mssql_num_rows($orders) == 0) {
            echo "No previous orders found.";
        } else {

        //OUTPUT PREVIOUS ORDERS
        echo "Last 10 Orders<br />";
        echo "<table class='orders'>";
        echo "<tr><td>Date</td><td>Job Number</td><td>Description</td><td>Status</td><td>Value</td></tr>";

        // ALTERNATE ROW COLOURS
        $c = 0;

          while ($row = mssql_fetch_array($orders)) {

            $class = $c++ % 2 == 1 ? "odd" : "even"; 
            echo "<tr class='$class'><td>"; 
            echo $row['CreateDateTime'];
            echo "</td><td>"; 
            echo $row['JobNo'];
            echo "</td><td>";
            echo $row['JobDesc'];
            echo "</td><td>"; 
            echo $row['Description'];
            echo "</td><td>";
            echo $row['PriceEst'];
            echo "</td></tr>";
          }  

        echo "</table>";

        }

        mssql_free_result($orders);

        ?>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td style="width:40%">
                            <table class="data" style="width:100%">
                                <tr class="theader">
                                    <td>Comments Log</td>
                                </tr>
                                <tr>
                                    <td>
        <?php

        // GET CURRENT COMMENTS
        $comments = mysql_query("SELECT * FROM comments WHERE customer_id='$customer' ORDER BY id DESC LIMIT 0,5")
        or die(mysql_error());

        // CHECK FOR 0 COMMENTS
        if(mysql_num_rows($comments) == 0) {
            echo "<p>No comments about this customer.</p>";
        }

        // LOOP THROUGH PREVIOUS COMMENTS
        while ($row = mysql_fetch_array($comments)) {
            echo "<p style='line-height:20px;'><b>" . $row['rep'] . "</b> <i>(" . $row['date'] . " at " . $row['time'] . ")</i><br />";
            echo $row['comment'] . "</p>";
        }

        ?>
                                    <form method="POST" name="comments" action="../assets/comment_post.php" onsubmit="return validateCForm();">
                                        <textarea rows="10" cols="45" name="comment_box"></textarea><br />
                                        <input type="hidden" name="comment_date" value="<?php echo date('d.m.Y'); ?>">
                                        <input type="hidden" name="comment_time" value="<?php echo date('H:i:s'); ?>">
                                        <input type="hidden" name="id" value="<?php echo $customer; ?>">
                                        <input type="hidden" name="rep" value="<?php echo $rep; ?>"><br />
                                        <input type="submit" class="search" name="submit" value="Post Comment">
                                    </form>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style="width:60%">
                            <table class="data" style="width:100%">
                                <tr  class="theader">
                                    <td>Customer Estimates History</td>
                                </tr>
                                <tr>
                                    <td>
        <?php

        // BUILD NEW QUERY FOR QUOTES
        $quotes = mssql_query("SELECT * FROM MainEstimateDetails WHERE CustomerRef = '$code' ORDER by EstimateDate DESC LIMIT 0,10")
        or die (mssql_error());

        // CHECK FOR 0 QUOTES
        if(mssql_num_rows($quotes) == 0) {
            echo "No previous quotes found.";
        } else {

        // OUTPUT PREVIOUS ORDERS
        echo "<table class='orders'>";
        echo "<tr><td>Date</td><td>Job Number</td><td>Description</td><td>Status</td><td>Value</td></tr>";

        // ALTERNATE ROW COLOURS
        $c = 0;

        while ($row = mssql_fetch_array($quotes)) {

            $class = $c++ % 2 == 1 ? "odd" : "even"; 
            echo "<tr class='$class'><td>"; 
            echo $row['CreateDateTime'];
            echo "</td><td>"; 
            echo $row['JobNo'];
            echo "</td><td>";
            echo $row['JobDesc'];
            echo "</td><td>"; 
            echo $row['description'];
            echo "</td><td>";
            echo $row['PriceEst'];
            echo "</td></tr>";
          }  

        echo "</table>";


        }

        ?>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </body>
        </html>

Thanks!

Upvotes: 1

Views: 1461

Answers (3)

Daniel Kingdon
Daniel Kingdon

Reputation: 21

I know this is a little late but I have just come accross this same issue and have found a fix.

In my case I could only use the * flag to indicate all fields when there were a small number of fields. On any table with a large number of fields (about 25 in my case) trying to return all fields would cause my entire php script to fail, no error just like yours. I have just been forced to specify fields manually for now.

Hope this helps.

Upvotes: 2

Exos
Exos

Reputation: 3988

First dont use mysql functions, try chage to mysqli object, or PDO. Second, escape your variables!!!!

$edit = mssql_query("SELECT * FROM Customers WHERE ID='$customer'");

for

$edit = mssql_query("SELECT * FROM Customers WHERE ID='" . mysql_real_escape_string($customer) . "'");

For view the error, you can enabled the display, in php.ini:

display_errors = On

or in php code script:

ini_set('display_errors', 'On');

And manage wath error display with error_reporting:

error_reporting (E_ALL); // For all

If is production and not can display error, try loggin this, from php.ini:

log_errors = On
log_errors_max_len = 0 // For not limits
error_log = /my/logfile.log

Upvotes: 0

Shef
Shef

Reputation: 45599

Why are you requiring twice require('../assets/mssql_connect.php');? Try to remove the second require, and see what you get from there.

Upvotes: 0

Related Questions