-2

Eperimenting PHP just for fun, But As being newbie, I'm unable to understand curcial parts of PHP....Please help me to sort out this problem which I'm explaining by example :

Suppose

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

So I want to display like this :

3 Records with uid details...

jay,vicky, sumair and 17 others like this.

Please help me to get output of something like this !!

Thanks !!

Charlie
  • 11,380
  • 19
  • 83
  • 138
  • 5
    Please use mysqli instead of mysql: http://stackoverflow.com/questions/8891443/when-should-i-use-mysqli-instead-of-mysql or consider PDO: http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/ – Ben Harold Jun 18 '13 at 01:13
  • you can't have 3 records with uid and then say 17 others – DevZer0 Jun 18 '13 at 01:20
  • @DevZer0 I want to display something like which facebook like function displaying like : sunil,zaheer and 7 other like this post. Isn't it possible to display ??? – sameer007860 Jun 18 '13 at 01:29
  • @sameer007860, Have you even read my answer? – Starx Jun 18 '13 at 01:33

2 Answers2

2

I can't stretch this enougth,

DO NOT USE MYSQL_* API anymore. [Read this]

It is VULNERABLE, mysqli_* functions are just as similar very little difference.

And You already are doing the things required for that output mysql_num_rows() already gives the number of total result. So:

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.";
}

Safer and MYSQLi Version

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.";
}
Starx
  • 77,474
  • 47
  • 185
  • 261
0

To understand the basics fundaments, I really recommend the official documentation PHP

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

A simple sample to execute a query and display the output:

$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";
Starx
  • 77,474
  • 47
  • 185
  • 261
well
  • 1,090
  • 11
  • 16