-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]);
    }
}

だから私はこのように表示したい:

uidの詳細を含む 3 つのレコード...

jay,vicky, sumair他 17 人がいいね!

このような出力を得るのを手伝ってください!!

ありがとう !!

4

2 に答える 2

2

私はこれを十分に伸ばすことはできません。

MYSQL_* API はもう使用しないでください。[これを読む]

これはVULNERABLEです。mysqli_*関数は同じようにほとんど違いがありません。

そして、あなたはすでにその出力に必要なことを行っており、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 に答える