I have the below code that grabs all the data I need, but as there are duplicate names in each row, I only want to display this once as a heading, and then display the array.

echo "<h1>Results Summary</h1>";
$psba = $_POST['psba'];
$con = mysql_connect("localhost","root","");
mysql_select_db('project', $con) or die('Cant Connect');
$result = mysql_query("SELECT * FROM mpf WHERE psba = '".$psba."'");
echo "<h1>".$psba."</h1><br />";
while($row = mysql_fetch_array($result))
$pair = $row['pair'];
$project =$row['project'];
$psba = $row['psba'];
$hostname = $row['name'];
$circuit = $row['circuit'];
echo $pair;
echo $hostname;
echo $circuit."<br />";

so shows this:

1Chepstow Community Hospital CCHLLSW0508002
2Chepstow Community Hospital CCHLLSW0508004
3Chepstow Community Hospital CCHLLSW0508001
4Chepstow Community Hospital CCHLLSW0508003

the first row and last row are the ones I need, but I have to have the hostname in the array, otherwise I cant display it?

I need it too grab the data, display $hostname as a heading, then the $pair and $circuit in the loop..

I cant seem to work it out,


4 Years
Discussion Span
Last Post by minitauros

Well if you need only one row, using the mysql extension you could do the following:

$query = 'SELECT something 
    FROM table 
    LIMIT 1';
$result = mysql_query($query) or die(mysql_error());
$fetch = mysql_fetch_assoc($result); // Contains only the first row, as we're not using it in a loop.

// $fetch now contains the first row. We have added a ORDER BY to your query and a LIMIT 1.
// This ensures that only one result, and, because of the ORDER BY, only the most recent
// result will be fetched (if "id" is your auto_increasing primary key field).

What your while() loop actually does is traverse to your result set and, after each loop execution, place the internal marker (which points to which result of the result set is being used) one further.

So, if your result sets contains 5 rows, for example, what it does is:

    // Do stuff with first record.

    // ...

   // Loop finishes, on to the next record! 
   // This continues until the internal point can go no further, which is when the 5th item has been parsed. Your $fetch would then contain the data of the last element in the returned records set.


Is this of any help? I'm not 100% sure I fully understand what you want to do :).

Edited by minitauros

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.