while($ row = mysql_fetch_row($ result))

时间:2014-05-28 19:46:07

标签: php mysql

我正在尝试使用php从mysql数据库中提取所有记录,并将其显示为格式化。由于某种原因,第一条记录正确显示,但直接显示在其下方的第二条记录不显示字段名称。我做错了什么?

<?php
        $db_host = 'localhost';
        $db_user = 'root';
        $db_pwd = '***';
        $database = 'db';
        $table = 'upstable';
        if (!mysql_connect($db_host, $db_user, $db_pwd))
            die("Can't connect to database");

        if (!mysql_select_db($database))
            die("Can't select database");
        $result = mysql_query("SELECT * FROM {$table}");                              // sending query

        if (!$result) {
            die("Query to show fields from table failed");
        } 
        $fields_num = mysql_num_fields($result);
        while($row = mysql_fetch_row($result))                                        // printing table rows
        {
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[0]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[1]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[2]v</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[3]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[4]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[5]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[6]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[7]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</div></b><div id='column2'>$row[8]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[9]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[10]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[11]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[12]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[13]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[14]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[15]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column2'><b>{$field->name}:</b></div><div id='column2'>$row[16]</div><br>";
            $field = mysql_fetch_field($result);
            echo "<div id='column'><b>{$field->name}:</b></div><div id='column'>$row[17]</div><br><br><br>";
            $row = 0;
        }
        mysql_free_result($result);
        ?>

更新:

几乎已修复,但不太对劲。它使每个完整的数据集交替背景(类“column2”为灰色,“columnn”为白色),而不是每组中的每一行。那更有意义吗?

这就是我现在正在使用的,我应该改变什么?

$row_num = 0;

$css_classes[0] = 'column2';
$css_classes[1] = 'column';

while($row = mysql_fetch_assoc($result))
{
    $row_num++;

    $class = $css_classes[$row_num % 2];

    foreach ($row as $col_name => $col_val)
    {
        echo "<div class='$class'><b>{$col_name}:</b></div><div class='$class'>$col_val</div><br>";
    }

echo "<br><br>";

3 个答案:

答案 0 :(得分:3)

下面的代码应该可以正常运行,并且具有更高的可读性。更新

好吧,我不擅长HTML / CSS。如果我是你,我想要这样。

    while($row = mysql_fetch_assoc($result))
    {
        foreach ($row as $col_name => $col_val)
        {
            echo "<div id='column2'><b>{$col_name}:</b></div><div id='column2'>$col_val</div><br>";
        }

    }

答案 1 :(得分:0)

您只获取第一行(因为$ results默认位置为[0],这是第一行。)。

你显然不了解获取排序和使用php显示数据所涉及的过程,但这里是使用mysqli准备好的语句和普通查询的示例。

mysqli查询

function getmarkets()
{
    global $mysqli;
    $query = $mysqli->query("SELECT pair, last_price FROM all_currency_data");
    $num_rows = $query->num_rows;
    if($num_rows > 0) {
        while($row = $query->fetch_assoc())
        {
            $mkt  = explode("-",$row["pair"]);
            $coin = $mkt[0];
            $base = $mkt[1];
            echo '<a class="animate" href="?p=trade&market='.urlencode($row["pair"]).'" style="text-decoration: none;"><li class="mkt-normal" id="'.$row["pair"].'_div">'.$coin.'/'.$base.' <span class="price" id="'.$row["pair"].'_price">'.$row["last_price"].'</span></li></a>';
        }
        $query->close();
    }

}

准备好的陈述

$stmt = $mysqli->prepare("SELECT user,message,timestamp FROM uc_support_replies WHERE tickethash = ? ORDER BY timestamp ASC");
    $stmt->bind_param('s',$ticket);
    $stmt->execute();
    $stmt->bind_result($user,$message,$timestamp);
    while($stmt->fetch())
    {
        if(in_array($user, $admins)) {
            $color = '0404B4';
        }else{
            $color = '2E2E2E';
        }
        $output .=
        '
        <div class="msgdiv">
        <hr class="five">
        <span class="user_name" style="color:#'.$color.'"><u>'.$user.'</u></span>
        </br>
        <span class="user_message">'.$message.'</span>
        </br>
        <span data-livestamp="'.$timestamp.'">'.$timestamp.'</span>
        <hr class="five">
        </div>
        ';
    }

答案 2 :(得分:-2)

MySQL扩展程序自2012年底的PHP 5.5.0起正式弃用,将来会被删除。推荐替代方案,因为PHP 5及更高版本是MySQLi(&#34;我&#34;代表&#34;改进&#34;)。 http://www.php.net/manual/en/book.mysqli.php