0

我的 php 代码有问题。问题是过滤指定的 WHERE 子句,但也吐出一些错误,第三条记录应该消失。这是代码:

<body>
<?php
$username="USERNAME";
$password="PASSWORD";
$database="DATABASE";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM searchacts";
$result=mysql_query($query);
$num=mysql_numrows($result);
$result = mysql_query("SELECT * FROM searchacts
WHERE category='Party Bands'");
mysql_close();
?>
<?php
$i=0;
while ($i < $num) {

$image=mysql_result($result,$i,"image");
$name=mysql_result($result,$i,"name");
$category=mysql_result($result,$i,"category");
$description=mysql_result($result,$i,"description");
$stamps=mysql_result($result,$i,"stamps");
$stickmen=mysql_result($result,$i,"stickmen");
$price=mysql_result($result,$i,"price");
$view=mysql_result($result,$i,"view");
$actpagelink=mysql_result($result,$i,"actpagelink");
?>
<a href="<?php echo $actpagelink; ?>" class="searchitem">
<div class="searchimage"><img src="<?php echo $image; ?>"/></div>
<div class="searchtext">
  <div class="searchname"><?php echo $name; ?></div>
  <div class="searchcategory"><?php echo $category; ?></div>
  <div class="searchdescription"><?php echo $description; ?></div>
</div>
<div class="searchstamps"><img src="<?php echo $stamps; ?>" /></div>
<div class="searchstickmen"><img src="<?php echo $stickmen; ?>" /></div>
<div class="searchprice"><span class="pricefrom">from</span><?php echo $price; ?></div>
<div class="searchview"><img src="<?php echo $view; ?>" /></div>
</a>
<?php
$i++;
}
?>
</body>

它可能非常简单,这是错误:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 3 in /home/enterta1/public_html/searchtestingv1.php on line 31

持续多行

4

4 回答 4

0

你有一个错字

 $num=mysql_numrows($result);

应该

$num = mysql_num_rows($result);
于 2013-02-24T00:00:03.247 回答
0

您正在查找行数

$num=mysql_numrows($result);

在您上次查询之前,因此 $num 不代表您上次查询返回的行数。

$result = mysql_query("SELECT * FROM searchacts WHERE category='Party Bands'");
于 2013-02-24T00:00:36.957 回答
0

WHERE看起来不错,但是……对我来说,这是从数据库中提取数据的一种奇怪方式。

做这个:

  1. 放在mysql_close()文件的末尾

  2. 没必要得到$num

  3. 行后$result=...做:

    while ($row=mysql_fetch_assoc($result)) $acts[]=$row;
    
    // now all your records are saved in array $acts, e.g.
    // $acts[0]['name'], $acts[0]['category'], where...
    // the first dimension of the array [0] contains the number of the record...
    // the second the field-name from your database!
    
  4. 输出所有 $acts 做:

    foreach ($acts as $act) { ?>
    
        ...
        <div class="searchimage"><img src="<?=$act['image']?>"/></div>
        <div class="searchtext">
            <div class="searchname"><?=$act['name']?></div>
            <div class="searchcategory"><?=$act['category']?></div>
            <div class="searchdescription"><?=$act['description']?></div>
        </div>
        ... <? // you get the idea ;-)
    
    } // foreach
    
于 2013-02-24T00:01:18.887 回答
0

$num在以下语句中计算所有获取的集合/行:

SELECT * FROM searchacts

同时,您正在尝试仅为其中选定的几个输出数据:

SELECT * FROM searchacts WHERE category='Party Bands'

因为,第二个查询的结果总是小于或等于第一个;你得到那个错误。

于 2013-02-24T00:08:57.423 回答