0

I have a php script to run mssql queries. When I run a query like, "SELECT first_name, last_name, mi FROM users", all I get back is 'first_name'. Here's the script:

<?php

require '../../scripts/database_connection.php';

$query_text = $_REQUEST['query'];
$result = mssql_query($query_text);

 if (!result) {
   die("<p>Error: " . $query_text ."</p>");
}

$return_rows = true;
 if (preg_match("/^\s*(CREATE|INSERT|UPDATE|DELETE|DROP)/i,
   trim(strtoupper($query_text)))) {
  $return_rows = false;
}

if ($return_rows) {
 echo "<p>Results from your query:</p>";
 echo "<ul>";

while ($row = mssql_fetch_row($result)) {
 echo "<li>{$row[0]}</li>";
}

 echo "</ul>";
}

 else {

  if ($result) {
   echo "<p>Your query was processed successfully.</p>";
   echo "<p>$query_text</p>";
 }
}

?> 

Any suggestions?

3
  • 2
    You always echo only the first element ($row[0]) Commented May 23, 2013 at 12:08
  • 1
    taking the mssql query via query string and execute it, dont this seem like a big security hole? Commented May 23, 2013 at 12:08
  • it absolutly IS a huge security risk. I assume it is only a local test script. If not, never accept outside parameters ($_REQUEST) as input for executable code (like a query) and never use unfiltered variables. Commented May 23, 2013 at 13:17

2 Answers 2

1

A $row is an array with all columns of your select. Just iterate with foreach or implode it:

while ($row = mssql_fetch_row($result)) {
    echo "<li>".implode(" - ", $row)."</li>";
}

You might also want to use mssql_fetch_assoc to get the respective column names:

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

Comments

0

Did you try to access $row[1] and $row[2]?

    while ($row = mssql_fetch_row($result)) {
     echo "<li>{$row[0]} - {$row[1]} - {$row[2]}</li>";
    }

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.