我有一个PHP脚本来运行mssql查询。当我运行一个类似“SELECT first_name,last_name,mi FROM users”的查询时,我得到的所有内容都是'first_name'。这是脚本:
<?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>";
}
}
?>
有什么建议吗?
答案 0 :(得分:1)
$ row是一个包含所有select列的数组。只需与foreach迭代或将其内爆:
while ($row = mssql_fetch_row($result)) {
echo "<li>".implode(" - ", $row)."</li>";
}
您可能还想使用mssql_fetch_assoc来获取相应的列名:
答案 1 :(得分:0)
您是否尝试访问$ row [1]和$ row [2]?
while ($row = mssql_fetch_row($result)) {
echo "<li>{$row[0]} - {$row[1]} - {$row[2]}</li>";
}