941,506 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 762
  • PHP RSS
Mar 5th, 2010
0

Issue with fetch row

Expand Post »
Hello there, I'm not getting the result's I want with my code.

So this is my table:

P_ID Latitude Longitude
9001 15 10
9002 23 54
9003 2 23
9004 23 12
9005 54 23

And here is my code:
PHP Syntax (Toggle Plain Text)
  1. //connect
  2. //ordering the latitude
  3. $query="SELECT Latitude FROM Position ORDER BY Latitude ASC";
  4. $result=mysql_query($query);
  5.  
  6. if (!$result) {
  7. echo 'Could not run query: ' . mysql_error();
  8. exit;
  9. }
  10. $row = mysql_fetch_row($result);
  11.  
  12. echo $row[0];
  13. echo $row[1];
  14. echo $row[2];

Issue: I want to be able to retrieve any row I want but this code keeps giving me back only the first result. In this specific example the result is ''2'' which is the Latitude of the row 0 since I order the column ASC.
Do you have any suggestion ? To get other values?

Thank you
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pedroteixeira07 is offline Offline
9 posts
since Feb 2010
Mar 5th, 2010
0
Re: Issue with fetch row
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. //connect
  3. //ordering the latitude
  4. $query="SELECT Latitude FROM Position ORDER BY Latitude ASC";
  5. $result=mysql_query($query);
  6. if (!$result) {
  7. echo 'Could not run query: ' . mysql_error();
  8. exit;
  9. }
  10. while ($row = mysql_fetch_assoc($result)) {
  11. echo $row['Latitude']."<br />";
  12. }
  13.  
  14. ?>

Try the above code.

Go through the following
http://php.net/manual/en/function.mysql-fetch-assoc.php
Reputation Points: 16
Solved Threads: 19
Junior Poster
as.bhanuprakash is offline Offline
109 posts
since Dec 2009
Mar 5th, 2010
0
Re: Issue with fetch row
PHP Syntax (Toggle Plain Text)
  1. //connect
  2. //ordering the latitude
  3. $query="SELECT * FROM Position ORDER BY Latitude ASC";
  4. $result=mysql_query($query);
  5.  
  6. if (!$result) {
  7. echo 'Could not run query: ' . mysql_error();
  8. exit;
  9. }
  10. while($row = mysql_fetch_row($result))
  11. {
  12. echo $row['P_ID ']."<br>";
  13. echo $row['Latitude ']."<br>";
  14. echo $row['Longitude'];
  15. }
  16.  
  17. //echo $row[1];
  18. //echo $row[2];
try like this
Reputation Points: 12
Solved Threads: 27
Junior Poster
rajabhaskar525 is offline Offline
179 posts
since Nov 2009
Mar 5th, 2010
0
Re: Issue with fetch row
haaaa.......you printed only first array.
if you want to display all the rows which is in database then use while loop as banu said above.
Reputation Points: 21
Solved Threads: 35
Posting Pro
muralikalpana is offline Offline
534 posts
since Sep 2009
Mar 5th, 2010
0
Re: Issue with fetch row
Hi, thanks for your answers. What I want is to be able to select for example the first latitude or the forth latitude (ordered ASC), not necessarily the first one or all of them but a row[x]. Do you understand what I mean? Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pedroteixeira07 is offline Offline
9 posts
since Feb 2010
Mar 5th, 2010
0
Re: Issue with fetch row
Sounds like you'll need to either add a limit to the query:
PHP Syntax (Toggle Plain Text)
  1. $query="SELECT * FROM Position ORDER BY Latitude ASC LIMIT 4,1";
This will return 1 row, starting with the 4th result returned.

Or, use a while loop as mentioned above and count the results:
PHP Syntax (Toggle Plain Text)
  1. $i=0;
  2. while($row = mysql_fetch_row($result))
  3. {
  4. if ($i==3)
  5. {
  6. echo $row['P_ID ']."<br>";
  7. echo $row['Latitude ']."<br>";
  8. echo $row['Longitude'];
  9. }
  10. $i++;
  11. }
The first option is probably a lot more efficient, but both should work.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
conord is offline Offline
11 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php count
Next Thread in PHP Forum Timeline: Function Return (recursion error?)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC