Home > Backend Development > PHP Tutorial > 查询语句的问题

查询语句的问题

WBOY
Release: 2016-06-23 14:09:49
Original
1061 people have browsed it

数据库是mssql的。在查询分析器里运行该语句没问题。但是。我在网站里用结果就是不一样。

select top 6 * from WEB_VIEW  where DNBH='$username' order by SFMonth desc 

在mssql查询分析器里,运行,得到6条最新的结果, 但是,在页面上测试,就只能得到5条记录,少了最新那一条。

难道6 改为 7 ?

//connection to the database$dbhandle = mssql_connect($myServer, $myUser, $myPass)  or die("连接不上数据库"); //select a database to work with$selected = mssql_select_db($myDB, $dbhandle)  or die("连接不上指定数据库"); $username=trim($_POST['username']);$password=trim($_POST['pass']);//declare the SQL statement that will query the database$query = "select top 6 * from WEB_VIEW  where DNBH='$username' order by SFMonth desc ";//execute the SQL query and return records$result = mssql_query($query) or die ("查询数据失败: ".mysql_error());$numRows = mssql_num_rows($result); $nRow=mssql_fetch_array($result);if ($password <> substr($nRow[3],-4)) {echo "<h2>输入有误,请正确输入用户名和密码。谢谢!</h2>";}else	{    if (empty($numRows)) echo "<h2>没有查到数据哦,请正确输入用户名和密码。谢谢!</h2>";	else {	//$nRow=mssql_fetch_array($result);	echo "你好!电脑编号为";	echo "$username";	echo ",";	echo "户名";	echo "$nRow[2]";	echo "<table border='1'>			 <tr>				 <th>月份</th>				 <th>总费用</th>			</tr>	";	while($row=mssql_fetch_array($result))	{		echo"<tr>";		echo"<td>";		echo $row[1];		echo"</td>";		echo"<td>";		echo ($row[10]+$row[4]);		echo"</td>";		echo"</tr>";	}	mssql_close($dbhandle);    }}
Copy after login


回复讨论(解决方案)

第18行有 $nRow=mssql_fetch_array($result); 读取了第一条记录
所以就少了一条
38到48行的代码应改为
    do{
        echo"

";
        echo"";
        echo $row[1];
        echo"";
        echo"";
        echo ($row[10]+$row[4]);
        echo"";
        echo"";
    }while($row=mssql_fetch_array($result));

即使用 do wuile 循环,而不是 while 循环

那个为了密码的判断的,我应该可以不用查询两次吧。

我再调整一下。

用两句sql来查算了。想不到什么好办法。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template