PHP数组不显示数据库中的所有数据 [英] PHP Array doesn't show all the data from database

查看:140
本文介绍了PHP数组不显示数据库中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本和简单的脚本,应该显示我的数据库中的记录。问题:它不显示所有的记录。我试过它,即使有最简单的mysql
($ sql =SELECT * FROM $ tbl_name;),但仍然有一些记录丢失(大部分是未显示的第一个列表)。

I have a very basic and simple script that should display records from my database. The problem: it doesn't show all the records. I've tried it even with the most simple mysql ($sql="SELECT * FROM $tbl_name";) but still some records are missing (mostly the first of the list that isn't shown).

这里是我的代码(全部在1页):

So here is my code (it's all on 1 page):

<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$tbl_name="***";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE rowNameOne >= 0.01 AND rowNameTwo='2013'";

if ($_GET['sort'] == 'one')
{
    $sql .= " ORDER BY one ASC";
}
elseif ($_GET['sort'] == 'two')
{
    $sql .= " ORDER BY two ASC";
}
elseif ($_GET['sort'] == 'three')
{
    $sql .= " ORDER BY three ASC";
}
elseif($_GET['sort'] == 'four')
{
    $sql .= " ORDER BY four ASC";
}
elseif($_GET['sort'] == 'five')
{
    $sql .= " ORDER BY five ASC";
}

$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>

<body onload="parent.alertsize(document.body.scrollHeight);"> 
<br />
<table cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" colspan="5">
<font>Titel</font>
</td>
<tr>
<td><a href="pageName.php?sort=one">Titel one</a></td>
<td><a href="pageName.php?sort=two">Titel two</a></td>
<td><a href="pageName.php?sort=three">Titel three</a></td>
<td><a href="pageName.php?sort=four">Titel four</a></td>
<td><a href="pageName.php?sort=five">Titel five</a></td>
</tr>
<tr>
<td colspan="5" class="noBorder">

<?php
while($rows=mysql_fetch_array($result)){
?>

<a href="pageName.php?id=<? echo $rows['id']; ?>" >
<table width="100%">
<tr>
<td><? echo $rows['rowNameOne']; ?></td>
<td><? echo $rows['rowNameTwo']; ?></td>
<td><? echo $rows['rowNameThree']; ?></td>
<td><? echo $rows['rowNameFour']; ?></td>
<td><? echo $rows['rowNameFive']; ?></td>
</tr>
</table>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
<?php
}
?>
</a>

</td>
</tr>
</table>

这是一个非常基本的代码,很容易,我可以说,但仍然缺少记录,显示数据库中的所有内容。我做错了什么?

It's a very basic code, easy as can be I would say, but still it's missing records, not displaying everything that's in the database. What am I doing wrong?

感谢您的帮助!

推荐答案

在开始循环之前,您可以这样做:

Before you start the loop, you do this:

$rows=mysql_fetch_array($result);

然后循环条件为:

while($rows=mysql_fetch_array($result)){

所以第一个结果永远不会显示。

So the first result is never shown. I would advice to remove the first statement, since you're not using its results between that statement and the loop.

在相关的注意事项上,请考虑移动到 PDO mysqli

On a related note, please consider moving to PDO or mysqli.

这篇关于PHP数组不显示数据库中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆