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:

//connect
//ordering the latitude 
$query="SELECT Latitude FROM Position ORDER BY Latitude ASC";
$result=mysql_query($query);

if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0];
echo $row[1];
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

<?php
//connect
 //ordering the latitude
 $query="SELECT Latitude FROM Position ORDER BY Latitude ASC";
 $result=mysql_query($query);
 if (!$result) {
 	echo 'Could not run query: ' . mysql_error();
 	exit;
 }
while ($row = mysql_fetch_assoc($result)) {
 	echo $row['Latitude']."<br />";
}
 
?>

Try the above code.

Go through the following
http://php.net/manual/en/function.mysql-fetch-assoc.php

//connect
//ordering the latitude 
$query="SELECT * FROM Position ORDER BY Latitude ASC";
$result=mysql_query($query);

if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
while($row = mysql_fetch_row($result))
{
echo $row['P_ID ']."<br>";
echo $row['Latitude ']."<br>";
echo $row['Longitude'];
}

//echo $row[1];
//echo $row[2];

try like this

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.

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

Sounds like you'll need to either add a limit to the query:

$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:

$i=0;
while($row = mysql_fetch_row($result))
{
if ($i==3) 
{
echo $row['P_ID ']."<br>";
echo $row['Latitude ']."<br>";
echo $row['Longitude'];
}
$i++;
}

The first option is probably a lot more efficient, but both should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.