-2

实验 PHP 只是为了好玩,但作为新手,我无法理解 PHP 的基本部分......请帮助我解决这个问题,我正在通过示例进行解释:

认为

$sql = "SELECT id, text,uid FROM feeds WHERE uid='".$ud."' LIMIT 10";
$items = mysql_query($sql);
echo mysql_error();

if (@mysql_num_rows($items) > 0)
{
    while ($item = mysql_fetch_array($items))
    {
        $feed = $item[1];
        $nick = getnick($item[2]);
    }
}

所以我想这样显示:

3 条带有uid详细信息的记录...

jay、vicky、sumair 和其他 17 人喜欢这个。

请帮我得到这样的输出!

谢谢 !!

4

2 回答 2

2

我不能伸展这个足够的,

不再使用 MYSQL_* API。[读这个]

它是VULNERABLEmysqli_*功能一样相似,差别很小。

而且您已经在做该输出所需的事情mysql_num_rows()已经给出了总结果的数量。所以:

if (mysql_num_rows($items) > 0)
{
    $count = mysql_num_rows($items);
    echo $count." Records with uid details..."; //Display the count of records

    $threeNameHolder = array; // Hold the first three names on this

    while ($item = mysql_fetch_array($items))
    {
        $feed = $item[1];
        $nick = getnick($item[2]);
        if(count($threeNameHolder) < 3) {
            $threeNameHolder[] = $nick;
        } else break; // End the loop here
    }

    //Now display the name
    echo implode(",", $threeNameHolder). " and ".($count - 3)." others like this.";
}

更安全的 MYSQLi 版本

if (mysqli_num_rows($items) > 0)
{
    $count = mysqli_num_rows($items);
    echo $count." Records with uid details..."; //Display the count of records

    $threeNameHolder = array; // Hold the first three names on this

    while ($item = mysqli_fetch_array($items))
    {
        $feed = $item[1];
        $nick = getnick($item[2]);
        if(count($threeNameHolder) < 3) {
            $threeNameHolder[] = $nick;
        } else break; // End the loop here
    }

    //Now display the name
    echo implode(",", $threeNameHolder). " and ".($count - 3)." others like this.";
}
于 2013-06-18T01:26:44.003 回答
0

要了解基础知识,我真的推荐官方文档 PHP

http://www.php.net/manual/en/ref.mysql.php

执行查询并显示输出的简单示例:

$query = mysql_query("SELECT a, b FROM table_name WHERE c='".$something."' LIMIT 10");

$num_rows = mysql_num_rows($query);

$test = array(); // create a empty array
/* while there is result */
while ($item = mysql_fetch_array($items)){
    $columnA = $item[0];// first column (a)
    $columnB = $item[1]); // second column (b)
    $test[] = $columnB; // push_back a item on array
}

echo $num_rows. " Records with **" . $something . "**...";
echo implode($test, ", ") . "and some text";
于 2013-06-18T01:27:36.413 回答