【问题标题】:i only get one result using this code使用此代码我只得到一个结果
【发布时间】:2012-04-28 04:33:29
【问题描述】:
<a href="add.php">add new product</a><br>
<br>

<?php

include("mysql.php");

$result = mysql_query("SELECT * FROM gallery ");

$just = mysql_fetch_array($result);
$num=mysql_num_rows($result);  

$table="";
$table.="<td>delete</td>";

$table.="<td>update</td>";
if ($num > 0 ) {
$i=0;

while($just = mysql_fetch_array($result))   
{
$num=mysql_num_rows($result);  
  {

$table .= "<tr>";

$table .= "<td><a href=\"delete.php?name=".$just['name']."&id=".$just['id']."\">".$just['title']."</a></td>";
$table .= "<td><a href=\"update.php?description=".$just['description']."&title=".$just['title']."&id=".$just['id']."\">".$just['title']."</a></td>";

  }
$table .= "</tr>";
while ($i < $num) {

$name = stripslashes(mysql_result($result,$i,"name"));
$title = stripslashes(mysql_result($result,$i,"title"));
$description = stripslashes(mysql_result($result,$i,"description"));

++$i; }
}
}


else { $table = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; }

?>

<table border="1" cellpadding="1" cellspacing="2"><? echo $table ?></table>

早上好,在上面的代码中,我试图创建一个包含 2 列删除和更新的表,能够通过这个页面管理 mysql,但是我从 mysql 表中只得到 1 行,虽然我希望有 4 行(保存 4 行在mysql表中) 这里有什么问题,提前谢谢

【问题讨论】:

标签: php mysql row


【解决方案1】:
<a href="add.php">add new product</a><br>
<br>

<?php

include("mysql.php");

$result = mysql_query("SELECT * FROM `gallery`");

$num = mysql_num_rows($result);  

$table = "";
$table .= "<td>delete</td>";
$table.="<td>update</td>";

if ($num > 0 ) {
    $i=0;
    while($just = mysql_fetch_assoc($result)) {
        $num=mysql_num_rows($result);  
        $table .= "<tr>";
        $table .= "<td><a href=\"delete.php?name=".$just['name']."&id=".$just['id']."\">".$just['title']."</a></td>";
        $table .= "<td><a href=\"update.php?description=".$just['description']."&title=".$just['title']."&id=".$just['id']."\">".$just['title']."</a></td>";
    }
    $table .= "</tr>";
    while ($i < $num) {
        $name = stripslashes(mysql_result($result,$i,"name"));
        $title = stripslashes(mysql_result($result,$i,"title"));
        $description = stripslashes(mysql_result($result,$i,"description"));
        ++$i; 
    }
} else { 
    $table = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; 
}
?>

<table border="1" cellpadding="1" cellspacing="2"><? echo $table ?></table>

【讨论】:

  • 大家好,所有答案都是正确的谢谢,但最后一个给了我我的代码所需要的一切谢谢
【解决方案2】:

您获取结果并计算错误位置的行数,并且您有一个基本上什么都不做的子 while 循环。

在这里试试这个:

<?php
include("mysql.php");

$result = mysql_query("SELECT `id`,`name`,`title`,`description` FROM gallery");

$table=null;
if (mysql_num_rows($result) > 0 ) {
    while($just = mysql_fetch_assoc($result)){
        $table .= "<tr>".PHP_EOL;
        $table .= "<td><a href=\"delete.php?name=".$just['name']."&id=".$just['id']."\">".$just['title']."</a></td>".PHP_EOL;
        $table .= "<td><a href=\"update.php?description=".$just['description']."&title=".$just['title']."&id=".$just['id']."\">".$just['title']."</a></td>".PHP_EOL;
        $table .= "</tr>".PHP_EOL;
    }
}else{
    $table = '<tr><td colspan="2" align="center">Nothing found</td></tr>';
}
?>

<table border="1" cellpadding="1" cellspacing="2"><? echo $table; ?></table>

【讨论】:

    【解决方案3】:

    很好的方法,但它确实需要更好的实现。

    首先,给自己弄个函数,放在mysql.php中,方便经常使用。

    function sqlArr($sql){
      $ret = array();
      $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
      if ($res) {
        while($row = mysql_fetch_array($res)){
          $ret[] = $row;
        }
      }
      return $ret;
    }
    

    然后写一段代码来获取数据

    <?php
    include("mysql.php");
    $data = sqlArr("SELECT * FROM tbl_names");
    foreach ($data as $k => $value) $data[$k] = htmlspecialchars($value);
    include 'template.php';
    

    然后编写一个模板来完善你的 HTML 模板方法:

    <table border="1" cellpadding="1" cellspacing="2">
    <? if (!$data)): ?>
      <tr>
        <td colspan="2" align="center">Nothing found</td>
      </tr>
    <? else: ?>
    <?     foreach($data as $just): ?>
      <tr>
        <td><a href="update.php??id="<?=$just['id']?>"><?=$just['title']?></a></td>
      </tr>
    <?     endforeach ?>
    <? endif ?>
    </table>
    

    看:您的代码缩短了 2 倍,但可读性更高!

    请注意,您不应将整个数据传递给编辑脚本。只需要 id 就足够了!在更新脚本中从数据库中获取要编辑的数据。

    另外请注意,您不应该使用 GET 方法来删​​除记录 - 只能发布。因此,我建议您不要在表格中而是在更新表单中使用“删除”按钮。

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多