8 Replies - 898 Views - Last Post: 17 July 2011 - 01:16 AM Rate Topic: -----

#1 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

fetch data from database + only the rows needed

Posted 16 July 2011 - 12:00 AM

hi all,

I have a problem , do anybody know how to fetch data only the we needed.

If I explain this further,

I wanted to fetch data from usercomments table's 3rd row data then how can i get it?

plz reply,
thx in advance menuka.
Is This A Good Question/Topic? 0
  • +

Replies To: fetch data from database + only the rows needed

#2 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 12:03 AM

Here's an example:

$result = mysql_query("SELECT `blah`, `blah` FROM `table`");

while($row = mysql_fetch_assoc($result)) {
    // whatever you want to do
}


I suggest you do more research on mysql_query.

This post has been edited by EnvXOwner: 16 July 2011 - 12:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 03:18 AM

View PostEnvXOwner, on 16 July 2011 - 09:03 AM, said:

I suggest you do more research on mysql_query.

contrary, I suggest to research the modern (and not outdated) database classes from PHP (namely PDO and MySQLi), which have a lot more options to fetch a result (e.g. there is a method to fetch a column’s value directly)
Was This Post Helpful? 0
  • +
  • -

#4 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 05:10 AM

View Postmenukadevinda, on 16 July 2011 - 09:00 AM, said:

hi all,

I have a problem , do anybody know how to fetch data only the we needed.

If I explain this further,

I wanted to fetch data from usercomments table's 3rd row data then how can i get it?

plz reply,
thx in advance menuka.


If I understand you properly, I'd think you want to achieve this:
SELECT whatever column(s) FROM usercomments table
...
LIMIT 2,1;



The LIMIT <offset> <number of rows to return> clause is suited for such operations. It tells MySQL to begin its querying from <offset>th(0(zero) is the first row) row and return a particular number of rows indicated by <number of rows to return>.

So the above code will start querying at the third row and return only that row i.e 1 row. Then you can get whatever data you want from that row. Is this what you intended?

No offence but (If I was right about the proffered solution) your question seems a little fundamental concepts-based, so I suggest you take some time to go over such concepts again(2 weeks maybe) for all the languages and tools you use for development and you won't regret you did.

cheers.
Was This Post Helpful? 1
  • +
  • -

#5 c1ph3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-July 11

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 03:34 PM

Here you Go :)


$result = mysql_query("SELECT row_name FROM `table`");
while($row = mysql_fetch_array($result)) {
echo $row;
}



or

$result = mysql_query("SELECT row_name FROM `table`");
while(list($col2) = mysql_fetch_array($result)) {
echo $col2;
}



change $col2 with your field name.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 03:54 PM

View Postc1ph3r, on 17 July 2011 - 12:34 AM, said:

$result = mysql_query("SELECT row_name FROM `table`");
while($row = mysql_fetch_array($result)) {
echo $row;
}

let me guess … it prints "Array" ?
Was This Post Helpful? 1
  • +
  • -

#7 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 04:45 PM

thx all i have solve the probelem....
Was This Post Helpful? 0
  • +
  • -

#8 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: fetch data from database + only the rows needed

Posted 16 July 2011 - 07:38 PM

View PostDormilich, on 16 July 2011 - 06:54 PM, said:

View Postc1ph3r, on 17 July 2011 - 12:34 AM, said:

$result = mysql_query("SELECT row_name FROM `table`");
while($row = mysql_fetch_array($result)) {
echo $row;
}

let me guess … it prints "Array" ?


lol. I'll fix that.

$result = mysql_query("SELECT `row_name` FROM `table`");
while($row = mysql_fetch_array($result)) {
echo $row['row_name'];
}

Was This Post Helpful? 0
  • +
  • -

#9 c1ph3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-July 11

Re: fetch data from database + only the rows needed

Posted 17 July 2011 - 01:16 AM

Oh, I forgot to put the field_name.

Thanks, EnvXOwner for rectifying it. :)

This post has been edited by c1ph3r: 17 July 2011 - 01:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1