0

need to determine the numerical value of the offset of a row by using the value of a column entry in a sorted table.

Given, I know the unique username ($username) in the row of interest.
e.g. $username = "SnowWhite";

Given: it is guaranteed that the $username is in the database and will be in the results of my initial query that follows. Here is my initial query (that works):

$query  = "SELECT ALL  username,ticket_number, queue_time  FROM members WHERE queue_time !=0 ORDER BY queue_time";
$result = mysql_query($query);

What is working is a brute force php search of the $result array for the value of $username.

I would like to replace the php search logic with a query (or a recasting of the above query) that stores the integer value of the row offset within $result for the username column's value of $username into a variable, for example, $offset.

For example, if SnowWhite is in the third row of $result I expect the $offset == 2 (assuming a row index offset starting from 0).

Ultimately (this works) I will update the 'ticket_number' value of the selected row to be == $offset+1. by:

$query="update members set ticket_number='$offset+1' where username='$username'";
mysql_query($query); 

Current Working Logic

$query  = "SELECT ALL  username,ticket_number, queue_time  FROM members WHERE queue_time !=0 ORDER BY queue_time";
$result = mysql_query($query);
$i = 0;
while ($row = mysql_fetch_array($result)) {
  if ($row[username] == $userinfo)  //-- compare usernames
     {
   ++$i;
   $query="update members set ticket_number='$i+1' where username='$userinfo'";
   mysql_query($query);
   break;
   }
++$i;
}
mysql_free_result($result);

username ticket_number queue_time Doc 0 0 The 1st 3 not in sorted result Grumpy 0 0 Happy 0 0 Sleepy 1 111 The following are ni sorted result Bashful 2 222 Sneezy 0 333 SnowWhite ??? 444 This is the current user (assign ???=4) Dopey 0 555 EvilQueen 0 666

  • 'ALL' is the DEFAULT behaviour, right, so it's not necessary (and even a little confusing) to include it here. – Strawberry Nov 29 '13 at 15:07

1 Answers1

0

So with your table example:

username    ticket_number   queue_time
Doc             0               0                  The 1st 3 not in sorted result
Grumpy          0               0
Happy           0               0
Sleepy          1               111                The following are in sorted result
Bashful         2               222
Sneezy          0               333
SnowWhite       ???             444               This is the current user (assign ???=4)
Dopey           0               555
EvilQueen       0               666

how to assign the value 4 to the column ticket_number of the username SnowWhite, 4 being the rank of the row in a sorted selection of the initial rows:

first get the sorted result:

$result = mysql_query("SELECT * FROM table WHERE [here the condition for your sorted array]"); 
// don't forget to remove the []. they don't go there.
while ($row = mysql_fetch_assoc($result))
 {$array[] = $row;}

this should get you something like:

[0]   [username]        [Sleepy]
      [ticket_number]   [1]
      [queue_time]      [111]
[1]   [username]        [Bashful]
      [ticket_number]   [2]
      [queue_time]      [222]
[2]   [username]        [Sneezy]
      [ticket_number]   [0]
      [queue_time]      [333]
[3]   [username]        [SnowWhite]
      [ticket_number]   [NULL]
      [queue_time]      [444]

then foreach through your array

    foreach ($array as $number => $row)
     {if ($row[username] == $userinfo)
       {$result = mysql_query('UPDATE table SET ticket_numer=' . ($number+1) . ' WHERE username=' . $userinfo);}}

the row corresponding to username SnowWhite will get ($number + 1) (3 + 1) as ticket_number

  • Deep dive, it is having two problems (Issue 1) it is assigning the 'ticket_number' value == the relative index number (0,1,2,3,4) not the relative position in the sorted array (1,4,2,3). (Issue 2) I think it is storing a string {I did not tell you that ticket_number is of type int(11) ... in testing I tried {partial} SET ticket_number='$offset+10' and there was no difference in the values assigned from SET ticket_number='$offset' – BroncoBilly Nov 29 '13 at 18:04
  • for issue 2) sql will not add anything, you must change the value of $offset BEFORE sending it to mysql: $offset = 4; $offset = $offset + 10; mysql_query("SELECT * FROM table WHERE $offset") would result in the sql: "SELECT * FROM table WHERE 14" – Félix Adriyel Gagnon-Grenier Nov 29 '13 at 18:19
  • so what you want is the way to set the (1,4,2,3) values for each row? – Félix Adriyel Gagnon-Grenier Nov 29 '13 at 18:58
  • so what is this value? the number of rows before the entry in your ARRAY or the number of rows before the entry in your DATABASE? – Félix Adriyel Gagnon-Grenier Nov 29 '13 at 22:28
  • I just did a better job in the table --- "edit" my original question to see it (I hope you can edit it) – BroncoBilly Nov 29 '13 at 23:05
  • yeah, i could see it, it's much clearer now. This should work. – Félix Adriyel Gagnon-Grenier Dec 01 '13 at 19:13
  • Still, even I answered, I strongly encourage you to read this question: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496, especially the comments on the initial question, mysql_* extension is currently being deprecated, much more explications and guides in the comments – Félix Adriyel Gagnon-Grenier Dec 01 '13 at 19:16
  • hum... seriously guy... you can also break out of foreach... http://php.net/manual/en/control-structures.break.php – Félix Adriyel Gagnon-Grenier Dec 03 '13 at 14:36
  • Thanks - I am a serious guy; just wasn't thinking :) – BroncoBilly Dec 03 '13 at 17:00