Jump to content

Recommended Posts

Hello,

 

I am trying to ONLY display the row pin from table pins with the highest id

 

here is the code that i have so far

 

<?php

mysql_connect("mysql2.000webhost.com", "a4428795_admin2", "sc55207") or die(mysql_error());
mysql_select_db("a4428795_payment") or die(mysql_error());

$query = "SELECT pin, MAX(id) FROM pins GROUP BY pin";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "The highest id is  ". $row['MAX(id)']. " and has pin number: " .$row['pin'];
echo "<br />";
}


?>

 

it displays this

 

The highest id is 1 and has pin number: testkey1
The highest id is 2 and has pin number: testkey2
The highest id is 3 and has pin number: testkey3
The highest id is 4 and has pin number: testkey4
The highest id is 5 and has pin number: testkey5

If you're only picking 1 record from the database there's no need to create a loop. Additionally you should take advantage of ordering and limiting.

 

$query = "SELECT pin, id FROM `pins` ORDER BY id DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);

echo "The highest id is  " . $row['id'] . " and has pin number: " . $row['pin'];

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.