1

I have a table

id | field1 | field2 | ... | field[x-1] | field[x]
1  | val1   | val2   | ... | val[x-1]   | val[x]

And I'm doing this search

for($i = 1; $i <= $x; $i++){
   $getvalue = mysql_query("SELECT * FROM table WHERE id='1' AND field".$i." LIKE 'some_value%'")or die(mysql_error());
   while($row = mysql_fetch_array($getvalue)){
      $j=$i+1;
      $val = $row['field'.$j.''];
   }
}

Some values in the table (val[1-x]) will be the same but I need to get only the LAST value. Limit 1 doesn't seem to work.

Update. Unfortunately as David suggested, I can't change the database. It has to be as it is. I have a text file (a settings dump from a sensor) that I insert it line by line into the db and do a check to see if there are any errors. Most check run ok but I have a few lines in that I need to choose only the last one to do the check. I have about 10 lines like this

D?
String
R?
String
...
D?
String
R?
String

I'm interested in the last string after R?. I use explode () and each value checked that they are with in limits.

17
  • 3
    mysql_ is deprecated. Use PDO or mysqli_ instead... Commented Feb 26, 2016 at 15:10
  • I know that. I'm using it for something personal (old version of php and mysql to be able to use php_printer.dll) and doesn't need to be up to date.
    – xlucian
    Commented Feb 26, 2016 at 15:14
  • I think your for loop should be inside while loop not the other way round. btw mysql_* functions are removed from PHP.
    – bansi
    Commented Feb 26, 2016 at 15:14
  • In what way is LIMIT 1 not working as expected? How did you try to use it? That would indeed limit to only one record. Also, you need to define what "last" means. You're currently not actually sorting the records in any way, so "last" is arbitrary.
    – David
    Commented Feb 26, 2016 at 15:16
  • @David. Lets say in fieldA I have valW and in fieldB I have also valW (B>A). When searching for valW even if I have Limit 1 set I still get both valW from fieldA and fieldB, when the only one I need is the one from fieldB
    – xlucian
    Commented Feb 26, 2016 at 15:22

2 Answers 2

3

Using ORDER BY id DESC LIMIT 1:

To get last row (with highest=last=newest id), you should use this:

SELECT * FROM table ORDER BY id DESC LIMIT 1;

Your updated code (with mysqli_ extension) to get last row:

$query= mysqli_query("SELECT * FROM table ORDER BY id DESC LIMIT 1") or die (mysqli_error());
}

$lastRow = mysqli_fetch_row($query);

echo $lastRow['id']; //get the last id

Using MAX():

You also can do it using SQL MAX() function:

SELECT * FROM table WHERE id=(SELECT MAX(id) FROM table)
0

Managed to get it working as I need it by doing this:

for($i = 1; $i <= 250; $i++){
    $getvalue = mysql_query("SELECT * FROM full_dump WHERE file_name='".$_FILES['datafile']['name']."' AND field_".$i." LIKE ' 100,%'")or die(mysql_error());
    while($row = mysql_fetch_array($getvalue)){
        $i_temp = $i;
    }
}
if($i_temp != NULL){
    $getvalue = mysql_query("SELECT * FROM full_dump WHERE file_name='".$_FILES['datafile']['name']."' AND field_".$i_temp." LIKE ' 100,%'")or die(mysql_error());
    while($row = mysql_fetch_array($getvalue)){
        $r = $row['field_'.$i_temp.''];
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.